// javaScript is very flexible
// flexible == dangerous
// added ECMAScript 5
'use strict';
// 2. Variable
// let (added in ES6)
let globalName = 'global name';
{
let name = 'ellie';
console.log(name);
name = 'hello';
console.log(name);
console.log(globalName);
}
console.log(globalName);
// var (before ES6. don't ever use this!)
// var hoisting (move declaration from bottom to top)
// 어디에 선언했든지 간에 항상 선언을 맨 위로 끌어 올린다.
// has no block scope
// scope 를 철저하게 무시한다.
console.log(age);
age = 4;
console.log(age);
var age;
// 값을 선언하기도 전에 사용 하는 말도 안되는 행위가 가능했다.
// 3. Constant (read only)
const daysInWeek = 7;
const maxNumber = 5;
//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 type : number, string, boolean, null, undefined, symbol
// object, box container (primitive 여러개 묶어서 한 단위로 관리)
// function, first-class funtion
// function 도 다른 type처럼 변수에 할당이 가능
// 함수의 parameter 로도 전달이 가능
// 함수의 return type으로도 function을 return 할 수 있다.
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 value: infinity, -infinity, nAn(not a number)
const infinity = 1 / 0;
const negativeInfinity = - 1 / 0;
const nAn = 'not a number' / 2;
console.log(infinity);
console.log(negativeInfinity);
console.log(nAn);
// 기존에 js 에서 number 는 (-2^53 ~ 2^53) 까지만 유요했다.
// bigInt
// 숫자 끝에 n 만 붙여주면 된다.
// string
const char = 'c';
const brendan = 'brendan';
const greeting = 'hello ' + brendan;
console.log(`value: ${greeting}, type: ${typeof greeting}`);
const helloBob = `hi ${brendan}!`; //template literals
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;
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(`is same? `, symbol1 === symbol2); //false
//string 이 똑같다면 동일한 symbol을 만들고 싶을 때
const gSymbol1 = Symbol.for('id');
const gSymbol2 = Symbol.for('id');
console.log(`is same? `, gSymbol1 === gSymbol2); // true
// symbol 출력시 에는 string으로 변환 한 뒤에 출력!
console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);
// object
const ellie = {name: 'ellie', age: 20};
ellie.age = 40;
console.log(`object`);
console.log(`ellie's age : ${ellie.age}, type: ${typeof ellie.age}`);
// 5. Dynamic typing: dynamically typed language
let text = 'hello';
console.log(text.charAt(0));
console.log(`value: ${text}, type: ${typeof text}`);
text = 1;
console.log(`value: ${text}, type: ${typeof text}`); // number 형으로 바뀌었다.
text = '7' + 5;
console.log(`value: ${text}, type: ${typeof text}`); // string 으로 출력한다.
text = '8' / '2';
console.log(`value: ${text}, type: ${typeof text}`); // number 형으로 바꿔서 계산 뒤 출력한다.
console.log(text.charAt(0)); // error 발생!!!!!!!!! 앞에서 number 로 바꿨기 때문에!
// type script 의 중요성!!!!!!!!