반응형
드림코딩 엘리 - 자바스크립트 강의 #3
1. Use strict
- added in ES5
- use this for Vanilla JavaScript
- ex) 'use strict';
2. Variable, rw(read/write)
- let (added in ES6)
- var (don't ever use this!)
- var hoisting (move declaration from bottom to top) : 선언하기도 전에 할당할 수 있음
- has no block scope : (), {} 뚫고 나옴
3. Constant, read(read only)
- use const whenever possible.
- only use let if variable needs to change.
- ex) const daysInWeeks = 7;
- Note!
- Immutable data types: primitive types, frozen objects (i.e. object.freeze())
- Mutable data types: all objects by default are mutable in JS
- favor immutable data type always for a few reasons:
- security
- thread safety
- reduce human mistakes
4. Variable types
- primitive, single item (더이상 쪼갤 수 x 단위) : number, string, boolean, null, undefined, symbol
- object, box container
- function, first-class function
- data types for number
- C : short, int, long, float, double
- Java : byte, short, long, int, float, double
- JS : number
- ex) let a = 12; let b = 1.2;
- TS : number
- ex) let a: number = 12;
- number - special numeric values : infinity, -infinity, NaN
const infinity = 1/0;
const negativeInfinity = -1/0;
const nAn = 'not a number'/2;
- bigInt : 범위 초과하는 수 뒤에 n만 붙이면 bigInt 타입으로 인식. 크롬, 파이어폭스에서만 됨 (fairly new)
- string
- boolean
- null
- undefined
- symbol
5. Dynamic typing: dynamically typed language
let text = 'hello';
console.log(`value: ${text}, type: ${typeof text}`);
//⇒ value: hello, type: string
text = 1;
console.log(`value: ${text}, type: ${typeof text}`);
//⇒ value: 1, type: number
text = '7' + 5;
console.log(`value: ${text}, type: ${typeof text}`);
//⇒ value: 75, type: string
text = '8' / '2';
console.log(`value: ${text}, type: ${typeof text}`);
//⇒ value: 4, type: number
반응형
'프로그래밍 > JavaScript' 카테고리의 다른 글
[JS] 이벤트 위임을 활용한 Event Listener 중복 문제 해결 (0) | 2022.03.12 |
---|---|
[JS] 단축 평가(Short-Circuit Evaluation) 쉽게 이해하기 | &&, ||, 연산자 우선순위 (0) | 2022.03.07 |
[자바스크립트] class vs object, 객체지향 언어 클래스 정리 (0) | 2021.03.22 |
[자바스크립트] Arrow function (0) | 2021.03.22 |
[자바스크립트] operator, if, for loop, 코드리뷰 팁 (0) | 2021.03.21 |