본문 바로가기

전체 글69

JS의 type vs TS의 type JS는 Dynamic type TS는 Static type 이다 이게 무슨 뜻이냐 하면은 TS는 개발중에 타입을 체크하지만 JS는 실제로 런타임에 들어가야지 타입을 체크하기 때문에 실제 동작전에는 알 수가 없다 ​ 이렇게 간단한 함수에서 JS는 typeof로 타입을 체크해주는 별도의 if문을 넣어야 한다 하지만 TS는 타입의 지정으로 간단하게 넘긴다. function jsFunction(a,b) { if(typeof a != 'number' || typeof b != 'number') { throw new Error('잘못된 입력!'); } return a + b; } function tsFunction(a:number,b:number) { return a + b; } 1. any , unknown an.. 2021. 12. 9.
TS - type annoatation let a: number; a = 'hi'; a = 20; a의 타입을 number 타입으로 지정했다 ​ 이럴 경우 a에는 number 타입의 값만 할당 할 수 있다 ​ 따라서 배열은 let a: Array = [1,2,3] let a: number[] = [1,2,3] // 위의 방법보단 주로 이렇게 사용한다 함수도 마찬가지로 function hello(x:number) { console.log(x) } hello('hi')​ 2021. 12. 9.
JS - 전개연산자 전개연산자 전개연산자는 ... 이 키워드로 사용할 수 있다 const fruits = ['apple','banana','melon'] console.log(fruits) console.log(...fruits) function toObject(a,b,c) { return { a:a, b:b, c:c } } console.log(toObject(...fruits)) 위와 같이 ...을 사용하면 , 로 구분된 배열 데이터가 각각 전개된다 따라서 toObject 함수에 저렇게 넣게 된다면 각각 'apple','banana','melon' 이 배열 데이터가 매개변수로 각각 들어가 return 된다 이 결과를 전개연산자를 사용하지 않는다면 console.log(toObject(fruits[0],fruits[1],.. 2021. 10. 18.
JS - 구조 분해 할당 자바스크립트에서 객체나 배열안의 값을 변수로 분해할수 있게 해주는 문법이다. const user = { name:'Lee', age:20000, email:'Lee@KOR.co.kr', address:'KOR' } const {name:hi,age,email,address} = user console.log(`사용자의 이름은 ${hi}입니다`) console.log(`사용자의 나이는 ${age}입니다`) console.log(`사용자의 이메일은 ${email}입니다`) console.log(`사용자의 주소는 ${address}입니다`) 배열에서도 가능하다 const fruits = ['apple','banana','melon'] const [a,b,c] = fruits console.log(a,b,c) .. 2021. 10. 18.