("use strict"); //바닐라자바스크립트를 쓸 때 맨 위에 언급해서 좀 더 모던하게 쓰도록
# 2. Variable
# let (added in ES6)
let globaName = "global name";
{
let name = "CHY";
console.log(name);
console.log(globaName);
}
console.log(name);
console.log(globaName);
# var (dont ever use this!)
# var hoisting (어디에 선언했느냐에 상관없이 항상 제일 위에 선언을 끌어올리는 것)
# 그리고 {} 블록을 철저하게 무시한다. -> 좀 더 규모있는 프로젝트가면 선언하지도 않은 값들이 할당되는
# 큰 단점이 있었기 때문에 let이 나오게 되었다.
age = 4;
var age;
console.log(age); #선언을 뒤에 했는데도 잘 나옴
#3. Constants (read only)
#한 번 할당되면 할당할 수 없다.
#보안상으로 좋음
#스레드가 안정적으로 돌아감
#실수를 줄여준다.
const daysInWeek = 7;
#4. Avriable types
#primitive, single item: number, string, boolean, null, undefiend, symbol
#object, box container
#자바스크립트에서는 number 하나면 숫자는 끝!
let a = 12;
let b = 1.2; #어떤 숫자든 할당해주면 끝!
let c: number = 12; # 타입스크립트
# number - 숫자가 아닌 경우 - 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);
# 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; //false
console.log(`value: ${canRead}, type: ${typeof canRead}`);
console.log(`value: ${test}, type: ${typeof test}`);
#null
let nothing = null; # null로 값이 할당되어 있음
console.log(`value: ${nothing}, type: ${typeof nothing}`);
#undefined ,
let x; # 선언만 되어있고 할당이 되어있지 않음
console.log(`value: ${x}, type: ${typeof x}`);
#symbol, 고유한 식별자가 필요하거나 우선순위를 주고 싶을 때
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 나옴
#object
const CHY = { name: "hy", age: 31 }; #오브젝트 안의 name과 age는 재할당이 가능하다.
CHY.age = 23;
#5. Dynamic typing: 선언할 때 어떤 타입인지 설명하지 않고 프로그램이 동작할 때 값에 따라서 타입이 변경된다.
# 규모가 큰 프로젝트가 있을 때 오류가 생길 수 있다.
let text = "hello";
console.log(`value: ${text}, type: ${typeof text}`);
text = 1;
console.log(`value: ${text}, type: ${typeof text}`); # 1 , number
text = "7" + 5; #문자열에 숫자를 더하면 sring에 string더한값이 나옴
console.log(`value: ${text}, type: ${typeof text}`); # 75 , string
text = "8" / "2";
console.log(`value: ${text}, type: ${typeof text}`); # 4 , number
댓글