1. 대입 연산자
let b;
b = 1;
console.log(b);
1
b=3
console.log(b);
3
2. 산술 연산자
기본적인 사칙연산자 (+, -, *, /) 에 % 나눈 숫자의 나머지 ** 제곱
정상 계산 '숫자' - 숫자 = 숫자, '숫자' * 숫자 = 숫자, '숫자' / 숫자 = 숫자
단, '숫자' + 숫자 = 문자
console.log( '35 '+ 11)
3511
3. 단항 연산자
+(양수), -(음수) , !(부정)
숫자가 아닌 타입에 +(양수) 기호를 사용하여 숫자로 변환해 볼 수 있다.
console.log(+false); // 0
console.log(+null); // 0
console.log(+''); // 0
console.log(+true); // 1
console.log(+NaN); // NaN (Not a Number)
console.log(+'text'); // NaN
console.log(+undefined); // NaN
4. 할당 연산자
let a = 2;
a += 1; // a에 1을 더해라
console.log(a);
3
a *= 5; // a에 5를 곱해라
console.log(a);
15
5. 증감 연산자
let a = 1;
++a // a를 1씩 증가 시켜라
--a // a를 1씩 가소 시켜라
//주의
//a++ 필요한 연산을 하고, 그뒤 값을 증가(감소)시킴
//++a 값을 먼저 증가(감소)시키고 필요한 연산을 함
a = 0;
let b = a++;
console.log(b); a가 증가되기전에 출력되어서 값은 0
console.log(a); b에서 이미 a가 증가되었기 때문에 값은 1
a = 0;
let b = ++a; // 값이 증가 되고 필요한 연산을 하여서 두 값은 모두 1이된다.
console.log(b);
console.log(a);
6. 비교 연산자 (대,소,같음 비교)
7. 비교 연산자 (값의 같음 , 값과 타입이 모두 같음, 다름)
console.log(2 == 2); // true
console.log(2 != 2); // flase
console.log(2 == 3); // flase
console.log(2 != 3); // true
console.log(2 === '2'); // flase
console.log(2 !== '2'); // true
const obj1 = {
name: 'js',
};
const obj2 = {
name: 'js',
};
console.log(obj1 == obj2);
console.log(obj1 === obj2);
console.log(obj1.name == obj2.name);
console.log(obj1.name === obj2.name);
let obj3 = obj2;
console.log(obj2 == obj3);
console.log(obj2 === obj3);
(기초) 제어문 (0) | 2023.01.05 |
---|---|
(기초) 변수 타입 (0) | 2023.01.04 |
브라우저 좌표 구하기 (0) | 2022.11.28 |
윈도우 사이즈 구하기 (0) | 2022.11.28 |