자바스크립트(js) 1. data types, let vs var, hoisting 기록
자바스크립트 3. 데이터타입, data types, let vs var, hoisting | 프론트엔드 개발자 입문편 (JavaScript ES5+) by 드림코딩 by 엘리
// 1. Use strict
// added in ES 5
// use this for valina Javascript
'use strict';
// 2. Variable
// let (added in ES6=js)
let globalName = 'global name';
{
let name = 'elile';
console.log(name);
name ='hello';
console.log(name);
}
console.log(name);
console.log(globalName);
// var (Don't ever use this!)
// var hoisting (no matter where it is, move, declaration from bottom to top)
// has no block scope (ignore block scope)
{
age = 4;
var age;
}
console.log(age);
// 3. Constants
// favor immutable data type always for a few reasons:
// - security
// - thread safety
// - reduce human mistakes
const daysInWeek = 7;
const maxNumber = 5;
// in a word : Mutable type(let) / immutable type(const)
// 4. Variable types
// primitive, single item: number, string, boolean, null, undefined, symbol
// object, box container
// function, first-class function
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 bigInt = 1234567891234567891234567891234567890; // over (-2**53) ~ 2*53
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: ${helloBob}`);
//if you use '`', you don't have to '+' instead of it
console.log('value: ' + helloBob + ', type: ' + helloBob);
//boolean
//false: 0, null, undefined, NaN, ''
//true: any other value
const canRead = true;
const test = 3 < 1; //false
console.log(`value: ${test}, type: ${typeof test}`);
console.log(`value: ${canRead}, type: ${typeof canRead}`);
//null
let nothing = null;
console.log(`value: ${nothing}, type: ${typeof nothing}`);
//undefined
let x; //= undefined;
console.log(`value: ${x}, type: ${typeof x}`);
//symbol, create unique identifiers for objects
const symbol1 = Symbol('id');
const symbol2 = Symbol('id');
console.log(symbol1 === symbol2); //false
//no matter their string values are same, it's different
const gSymbol1 = Symbol.for('id');
const gSymbol2 = Symbol.for('id');
console.log(gSymbol1 === gSymbol2); //true
console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`);
//use description
// object(일상 생활에서 보는 물건과 물체들을 대표할 수 있는 것 ),
// real-life object, data structure
const ellie = { name: 'ellie', age: 20};
ellie.age = 21;
// 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}`);
text = '7' + 5;
console.log(`value: ${text}, type: ${typeof text}`);
text = '8' / '2';
console.log(`value: ${text}, type: ${typeof text}`);
source : www.youtube.com/watch?v=OCCpGh4ujb8