==
, ===
, !=
, !==
, <
, >
, <=
, >=
=
, +=
, =
, =
, /=
, %=
등&
, |
, ^
, ~
, <<
, >>
, >>>
typeof
, instanceof
delete
(객체의 프로퍼티 제거)Boolean(0) // false
Boolean([]) // true
Boolean("") // false
Boolean("0") // true
"5" - 1 // 4
"5" + 1 // "51"
null == undefined // true
for
, while
, do...while
if
, switch
forEach
: 배열을 순회for...in
: 객체의 키 순회for...of
: 배열(이터러블) 순회const arr = ['사과', '바나나', '체리'];
const obj = { a: 1, b: 2, c: 3 };
// for...in : 객체나 배열의 "키(인덱스)" 순회
console.log('for...in with array');
for (let index in arr) {
console.log(index, arr[index]); // index는 0, 1, 2
}
console.log('for...in with object');
for (let key in obj) {
console.log(key, obj[key]); // key는 'a', 'b', 'c'
}
// for...of : 배열이나 이터러블의 "값" 순회
console.log('for...of with array');
for (let fruit of arr) {
console.log(fruit); // '사과', '바나나', '체리'
}
// for...of는 객체에는 직접 사용할 수 없음 → 에러 발생
// for (let value of obj) { ... } ❌
// 객체에서 값을 순회하려면 Object.values(obj) 또는 Object.entries(obj) 사용
console.log('for...of with Object.entries');
for (let [key, value] of Object.entries(obj)) {
console.log(key, value); // 'a' 1, 'b' 2, 'c' 3
}
function sayHello(name) {
console.log('안녕, ' + name);
}
sayHello('강아지'); // 출력: 안녕, 강아지
sayHello
)const sayBye = function(name) {
console.log('잘가, ' + name);
};
sayBye('고양이'); // 출력: 잘가, 고양이
const add = (a, b) => {
return a + b;
};
console.log(add(2, 3)); // 출력: 5
한 줄이면 return 생략 가능
const square = x => x * x;
console.log(square(4)); // 출력: 16
function
키워드를 쓰지 않음this
가 바인딩되지 않음 (상위 스코프의 this
를 사용)구분 | 키워드 | 호이스팅 | this 바인딩 | 사용 예 |
---|---|---|---|---|
명시적 함수 | function | O | 함수 내부 this | 일반적인 함수 정의 |
익명 함수 | function | X | 함수 내부 this | 변수에 담아 사용하는 함수 |
화살표 함수 (람다) | => | X | 상위 스코프의 this | 짧고 간결한 함수, 콜백 등 |