// syntax part. 1 

( 변수, 데이터 타입, 연산자 )

 

 

1. 변수

// 상수
const 

// 변수 값을 변경해야할 때 사용
let
 
2. 원시 데이터 타입
// number
const num = 1;

// bigInt
2^53-1 보다 크거나 2^53-1보다 작은 숫자 
(number data type으로 표현이 불가능한 수)
const big_num = 123123123123123123123n;

// null : 값없음
// undefined : 값 지정 X

// Symbol : 객체의 고유한 식별자 (unique identifier)를 만들기 위해 사용  
const id = Symbol();
console.log(typeof id); // 'symbol'

// Symbol()의 인자로 이름 지정 가능
const id = Symbol('id');
console.log(id); // Symbol(id)
 
3. 참조 데이터 타입 (Reference Types)

// array, object
원시 데이터 타입이 할당될 때 변수에 값 자체가 담기는 것과 달리
보관하는 주소(Reference)가 담기는 데이터 타입이다.

 

// 배열

const arr = [1, 2, 3, 4];

 

// 객체

각 요소는 key: '값'으로 구성된다. (키,값 == property)

const user = {
	id: 'ayoung',
	pw: '1234',
	subjects: ['en', 'ko']
};

// 객체 접근 
user.id
user['id']


객체의 키는 문자 또는 심벌 데이터 타입이여야한다.

 

// Set
배열과 같은 데이터 집합, 배열과 달리 중복을 허용하지 않는다.
const set = new Set();
set.add(1);
set.add(2);
set.add(1); 
// Set(2) {1, 2}
// 셋에 이미 1이 들어있어서 중복허용 x

const arr = [1, 2, 1, 3, 4, 2, 1, 5];
const new_arr = new Set(arr);
console.log(new_arr); 
// Set(5) {1, 2, 3, 4, 5}

 

// Map

object와 같이 key: value을 연결하는 방식의 데이터 집합 

const map = new Map();
map.set('hello', 'js');
map.set(12, 34);
map // Map(2) { 'hello' => 'js', 12 => 34 }
// 객체 vs 맵
객체 : 키값 문자, 심벌타입만 허용
정렬에 관여하지 않음 
순회를 위해서 키값을 토해 배열을 얻어 배열을 순회(array loop)
크기를 알기 위해서는 키값 사용 등 직접 판별해야함

맵 : 키값으로 모든 데이터 타입 허용
삽입된 순서대로 정렬된다. 
map.size 로 크기를 알 수 있다.
맵 자체로 순회할 수 있음 

 

4. 연산자

// 나눗셈 /
// 나머지 %
// 지수 **

console.log('나눗셈: ' + 10 / 2); // 5
console.log('나머지: ' + 10 % 2); // 0
console.log('지수: ' + 10 ** 2); // 10^2

 

// 비교 연산자
// == (동등) : 값만 비교 두값이 같으면 t
// === (일치): 값, 타입이 엄격히 같으면 t
// != (부등) : 값만 비교 두값이 다르면 t
// !== (불일치) : 값, 타입이 엄격히 다르면 t
console.log('1 == "1": ', 1 == '1'); // t
console.log('1 === "1": ', 1 === '1'); // f


console.log('1 != "1": ', 1 != '1'); // f
console.log('1 !== "1": ', 1 !== '1'); // 자료형이 달라서 t
 
// 삼항 연산자
const result = 2 < 3 ? 'hello' : 'world'; 
console.log(result); // hello

 

// 널리쉬 연산자, 널 병합 연산자 (Nullish)
// 여러개의 피연산자 중 값이 확정되어 있는 피연산자를 찾는 연산을 수행한다,
const a = null;
const b = undefined;
const c = 'hello';
console.log(a ?? b ?? c); // hello

 

// 전개 구문
// 반복이 가능한 iterable 객체에 적용할 수 있는 문법으로 배열이나 객체, 문자열의 요소를 각각 꺼내서 전개한다.
const hello = 'Hello';
const world = 'World!';
console.log(...hello, ...world); // H e l l o W o r l d !

const arr = [10, 20];
function sum(a, b) {
  return a + b;
}
console.log(sum(...arr)); // 30


// sum(arr[0], arr[1]);
// 2개의 인자를 필요로 하는 함수 sum()을 호출할 때 
// 배열의 값을 각각 지정하지 않고 전개 구문을 사용해 인자를 전달한다.


// 전개구문을 사용해 배열 2개를 합쳐 새로운 배열 만들기
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const newArr = [...arr1, ...arr2];
console.log(newArr);

 

 

 

※ 자바스크립트 독학백서 정리 

+ Recent posts