티스토리 뷰

'use strict';
// var (don`t ever use this)
// var hoisting - 어디에 선언하는가에 관계 없이 선언을 가장 위로 끌어 올리는것
// has no block scope - 블럭 스콥의 개념이 없다.


// let - Mutable
// Variable - 변수 (변경될수 있는 값 read/write)
// let (added in ES6)
// global 변수는 어느곳에서나 접근이 가능하다.
// global 변수는 어플리케이션이 시작할때부터 끝날때까지 메모리에 올라가있기 때문에 최소한으로 사용해야한다.

let globalName = 'global';
{
  let name = 'ref';
  console.log(name);
  name = 'ref-m1';
  console.log(name);
  console.log(globalName);
}
console.log(name);
console.log(globalName);


// Immutable data types: premitive types, frozen objects
// 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

// Constant - Immutable (read only)
// const (added in ES6)
// - 보안, 변경되어야 할 이유가 없다면 const를 사용해야 한다.
const week = 7;
const maxNum = 10;

// Variable types
// primitive - single item: number, string, boolean, null, undefined, symbol
// object - box container: function, first-class function

const count = 10; // integer
const size = 10.5; // decimal number
console.log(`value: ${count}, type: ${typeof count}`);
console.log(`value: ${size}, type: ${typeof size}`);

// number - speicla numeric values: infinity, -infinty, 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 bigInt = 1234567890123456789012345678901234567890n; // over (-2**53) ~ (2**53)
console.log(`value: ${bigInt}, type: ${typeof bigInt}`);

// string
const char = 'c';
const brendan = 'brendan';
const greeting = 'hello' + brendan;
console.log(`value: ${greeting}, type: ${typeof greeting}`);
const helloBob = `hi ${brendan}`;
console.log(`value: ${helloBob}, type: ${typeof helloBob}`);

// boolean
// false : 0, null, undefined, NaN, ''
// true : any other value
const read = true;
const unRest = 3 < 1;
console.log(`value: ${read}, type: ${typeof read}`);
console.log(`value: ${unRest}, type: ${typeof unRest}`);

// null
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);

// undefined
let und;
console.log(`value: ${und}, type: ${typeof und}`);

// symbol, create unique identifiers for objects
// 고유식별자가 필요한 경우 사용
const symbol1 = Symbol('id');
const symbol2 = Symbol('id');
console.log(symbol1 === symbol2); // false
const symbol3 = Symbol.for('id');
const symbol4 = Symbol.for('id');
console.log(symbol3 === symbol4); // true

console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);


// 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}`);
text = '7' + 7;
console.log(`value: ${text}, type: ${typeof text}`);
text = '8' / 4;
console.log(`value: ${text}, type: ${typeof text}`);
console.log(text.charAt(0));


// object, real-life object, data structure
const user = { name: 'ref', age: 20};
user.name = 'ref-m1';
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함