変数の定理
1. Use strict
'use strict'
2. Variable, rw(read/write) 메모리에 읽고 씀
let (added in ES6)
let name = 'JJ';console.log(name); expect JJ
name = 'me';
console.log(name); expect me
block scope, global scope
block scope 는 {let name = 'ellie';}
global scope 는 let name = 'ellie';
창 밖에서는 안을 볼 수 없다. 하지만 안에서는 밖을 볼 수 없다.
var (don't ever use this)! 자바스크립트는 var hoisting(어디에 선언했건 가장 위로 끌어올림) 때문에 쓰지 않는다.
console.log(age);age = 4;
console.log(age);
3. Constants, r(read only) 메모리에 써져 있는 값을 읽을수만 있음.
use const whenever possible.
only use let if variable needs to change.
favor immutable data type always for a few reasons 항상 const를 사용하자
- security
- thread safety
- reduce human mistakes
const daysInWeek = 7;const maxNumber = 5;
4. Varialbe
-primitive, single item : number, string, boolean, null, undefiend, symbol
-object, box container
-object의 경우 너무 커서 한번에 메모리에 저장할 수 없다. 따라서 객체의 식별자는 ref를 가리킨다. 따라서 오브젝트의 프로퍼티가 변경되도 됨
-function, first-class function(함수도 변수이고 함수 변자로 함수를 넣을 수 도 있다)
primitive들
-number
const count = 17; integerconst size = 17.1; decimal number;
console.log(
value : ${count}, type : ${typeof count}
);console.log(
value : ${size}, type : ${typeof size}
) -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) ${string}
console.log( value : ${helloBob}, type : ${typeof helloBob}
); -boolean
false: 0, null, undefine, NaN, ''
true: any other value
const canRead = true;const test = 3 < 1; false
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, 심볼을 출력할때는 항상 symbol.description 으로 string으로 바꿔 줘야 함.
const symbol1 = Symbol('id');const symbol2 = Symbol('id');
console.log(symbol1 === symbol2) expect false
const symbol12 = Symbol.for('id');
const symbol23 = Symbol.for('id');
console.log(symbol12 === symbol23) expect true
5. Dynamic typing : dynamically typed language
let text = hello
;console.log(
value: ${text}, type : ${typeof text}
);text = 1;
console.log(
value: ${text}, type : ${typeof text}
);text = '7' + 5;
console.log(
value: ${text}, type : ${typeof text}
); expect 75JavaScriptは、実行時にタイプが特定されるため、多くのエラーが発生します.これもtypescriptを書く理由です.
Reference
この問題について(変数の定理), 我々は、より多くの情報をここで見つけました https://velog.io/@jj123/공부하기-variable-정리テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol