글로벌 변수는 프로그램 시작부터 끝까지 메모리에 탑재돼 있기 때문에 최소한으로 쓰는 게 좋다.
var(old): 선언 전에 값을 할당하거나 출력할 수 있다.
var (don't ever use this!)
var hoisting (move declaration from bottom to top)
has no block scope
(BABEL을 이용해 ES6로 개발하고 배포 시 ES5로 해도 된다.)
let : mutable data type
favor immutable data type always for a few reasons:
- security
- thred safety (다양한 스레드가 동시에 값을 변경하는 것을 막기 위해)
- reduce human mistakes
primitive(single item): number, string, boolean, null, undefined, symbol
object(싱글 아이템을 하나의 단위로 묶어놓은 것)
function(first-class function: 함수를 변수에 할당할 수 있으므로, 함수를 함수의 인자로 전달하거나 함수를 리턴할 수 있다.)
JavScript에서는 숫자 타입이 하나, number 뿐!
let a = 12;
let b = 1.2;
let a: number = 12;
et b: number = 1.2;
const infinity = 1 / 0; // Infinity
const negativeInfinity = -1 / 0 // -Infinity
const nAn = 'string' / 2 // NaN
사파리에서 지원 안 되는 최신 기능(숫자 뒤에 n입력 시 bigInt로 타입이 변경됨)
const int = 1; typeof int => number
const bigInt = 1n; typeof bigInt => bigInt
template literal
string ${value}
"string" + value
false = 0, null, undefined, NaN, ''
true = 1 and otehr values
null
let nothing = null;
undefined
let x;
const symbol1 = Symbol('string');
const symbol2 = Symbol('string');
symbol1 === symbol2 // false
const gSymbol1 = Symbol.for('string');
const gSymbol2 = Symbol.for('string');
gSymbol1 === gSymbol2 // true
JavaScript: Dynamically typed language (런타임에 할당된 값에 따라 타입이 변경될 수 있음, 유연하지만 큰 프로젝트 개발 시 문제가 있음)
Java, C: Statically typed language (변수 선언 시 어떤 타입인지 결정)
자바스크립트는 브라우저가 이해할 수 있는 언어기 때문에 실시간으로 연동해서 볼 수 있지만, 타입스크립트는 브라우저가 이해할 수 있는 언어로 trans compiler를 이용해야 되기 때문에 (BABEL),
자바스크립트를 먼저 배운 후 나중에 빠르게 TypeScript를 배우면 된다.
const ellie = { name: 'ellie', age: 20 };
ellie의 값은 변경할 수 없지만
ellie.name이나 ellie.age의 값은 변경할 수 있다.