TIL no.8

손병진·2020년 7월 20일
0

TIL

목록 보기
8/22

var & Hoisting

  • Hoisting : 아래 있는 선언을(만) 끌어올리다
function hello_1(){
	console.log('hello_1')
}

hello_1(); //'hello_1' 출력

hello_2(); //'hello_2' 출력(호이스팅 현상)

function hello_2(){
	console.log('hello_2')
}

  • var 에서도 호이스팅 현상 있다
age = 6;
age++;
console.log(age); // 7출력

var age;

  • 호이스팅 현상은 선언부만! 올라간다
console.log(name); // Undefined 출력

name = 'Mark';

console.log(name); // Mark 출력

var name = 'Kane' // 즉, name 선언부만 적용되고 'Kane' 정의는 적용되지 않은것.

  • let 호이스팅 적용 안됨
console.log(name); // error 발생 (name is not defined)

name = 'Mark';

console.log(name);

let name;

자료형

  • 동적 타이핑 : 정의되는 값에 따라서 type 달라진다
let whatever = 'Mark'; // 문자

whatever = 37; // 숫자

wharever = true; // bool

기본 타입(Primitive values)

Boolean

  • typeof : type 확인하는 키워드
const isTrue = true;
const inFalse = false;

console.log(isTrue, typeof isTrue); // true, boolean 출력
console.log(isFalse, typeof isFalse); // false, boolean 출력

const a = new Boolean(false); // 생성자

console.log(a, typeof a); //[Boolean:false]  object 출력

if (a){
	console.log('false?'); }
// 'false?' 출력
// a 값은 false 하지만 object 때문에 true 라고 인식된다

const b = Boolean(false); // 함수

console.log(b, typeof b); // false, boolean 출력

if (b){
	console.log('false?');}// 출력값 없음

Null & Undefined

const a = null;
console.log(a, typeof a); // null, 'object' 출력

let b;
console.log(b, typeof b); // undefined, 'undefined' 출력

if (a == b){
	console.log(a==b);} // true 출력
// null, undefined 같다고 인식

if (a === b){
	console.log(a===b);} // 출력값 없음
// null, undefined 같다고 인식(값 뿐만 아니라 타입도 비교)

Number

const a = 37;
console.log(a, typeof a); // 37 'number' 출력

const b = 96.7
console.log(b, typeof b); // 96.7 'number' 출력

const c = NaN
console.log(c, typeof c); // NaN 'number' 출력

const d = Number('Mark')
console.log(d, typeof d); // NaN 'number' 출력

const e = Number('37')
console.log(e, typeof e); // 37 'number' 출력

String

const a = 'Mark';
console.log(a, typeof a); // Mark string 출력

const b = 'Mark' + 'Lee';
const c = a + 'Lee';
console.log(c, typeof c) // Mark Lee string 출력

const d = `${a} Lee`;  // template string(ES6)
console.log(d, typeof d); // Mark Lee string 출력

Symbol(ECMAScript6)

const a = Symbol();
const b = Symbol(37);
const c = Symbol('Mark');
const d = Symbol('Mark');

console.log(a, typeof a); // Symbol() 'symbol' 출력
console.log(c === d); // false 출력

new Symbol(); // error : symbol is not constructor
  • 고유값을 만들때 주로 사용
  • 객체(object)에 대해서는 뒤에서 다시 다룰 예정
profile
https://castie.tistory.com

0개의 댓글