'use strict';
자바스크립트 제일 상단에 use strict
를 선언해주기!
added in ES 5
Why? 자바스크립트 언어를 만들 때 굉장히 빨리 만들어야 했기 때문에 유연하면서 위험한 언어,
개발자가 많은 실수를 할 수 있다는 말! 선언되지 않는 변수의 값을 할당한다던지, 기존에 존재하는 프로토타입을 변경한다던지의 비상식적인 것을 쓸수 없게 되는 명령어
let ( added in ES6) : mutable type
Block Scope { }
let globalName = 'global name';
{
let name = 'amanda';
console.log(name); // amanda
name = 'hello';
console.log(name); // hello
console.log(globalName); // global name
}
console.log(name); // ' '
console.log(globalName); // global name
global한 변수들은 어플리케이션이 실행될 때부터 끝날때까지 항상 메모리에 탑재되어 있기 때문에 최소한으로 쓰는 것이 좋고, 가능하면 클래스나 함수, If나 for로 필요한 부분에서만 정의해서 쓰는 것이 좋다.
var hoisting ( move declaration from bottom to top)
: 어디에 선언했냐와 상관없이 항상 제일 위로 선언을 끌어 올려주는 것을 말한다.
var has no block scope
{
age = 4;
var = age
}
console.log(age); // 4
favor immutable data type always for a few reasons :
-- security
-- thread safety
-- reduce human mistakes
const daysInWeek = 7;
const maxNumber = 5;
**
Immutable data types (변경X) : premitive types, frozen objects (i.e object.freeze())
Mutable data types (변경O) : all objects by default are mutable in JS
const count = 17; // integer
const size = 17.1; // decimal number
console.log(`value: ${count}, type: ${typeof count}`);
console.log(`value: ${size}, type: ${typeof size}`);
// number - special numeric values: infinity, -infinity, NaN
const infinity = 1 / 0;
const negativeInfinity = -1 / 0;
const nAn = 'not a number' / 2;
console.log(infinity);
console.log(negativeInfinity);
console.log(nAn);
// bigInt (fairly new, don't use it yet)
const bitInt = 12345678901234567890123456789012345678901234567890n;
console.log(`value: ${bigInt}, type: ${typeof bigInt}`);
Number.MAX_SAFE_INTEGER;
// string
const char = 'c';
const brendan = 'brendan';
const greeting = 'hello' + brendan;
console.log(`value: ${greeting}, type: ${typeof greeting}`);
const helloBob = `hi ${brendan}!`; // template literals (string)
console.log(`value: ${helloBob}, type: ${typeof helloBob}`);
console.log('value: '+ helloBob + ' type: ' + typeof helloBob);
// boolean
// false : 0, null, undefined, NaN, ''
// true : any other value
const canRead = true;
const test = 3 < 1; // true
console.log(`value: ${canRead}, type: ${typeof canRead}`);
console.log(`value: ${test}, type: ${typeof test}`);
// null
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);
// undefined
let x;
console.log(`value: ${x}, type: ${typeof x}`);
// symbol, create unique identifiers for objects
// 나중에 맵이나 다른 자료구조에서 고유한 식별자가 필요하거나
// 동시에 다발적으로(concurrent) 일어날 수 있는 그런 코드에서
// 우선 순위를 주고 싶을 때 정말 고유한 식별자가 필요할 때 쓰여진다.
const symbol1 = Symbol('id');
const symbol2 = Symbol('id');
console.log(symbol1 === symbol2); // false
const gSymbol1 = Symbol.for('id');
const gSymbol2 = Symbol.for('id');
console.log(gSymbol1 === gSymbol2); // true
console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);
// symbol 은 항상 .description을 이용해서 string으로 변환한 후 출력해야 한다.
// object, real-life object, data structure
const amanda = { name: 'amanda', age: 29 };
amanda.age = 29;
let text = 'hello';
console.log(text.charAt(0)); // h
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
console.log(text.charAt(0)); // Uncaught TypeError