개발자/JS

자바스크립트 3. 데이터타입 var let hoisting

엘리씨 2020. 12. 29. 00:22

1. js 파일 처음에 

 'use strict'; 선언하고서 strict하게.

 

2. variable->mutable type

let globalName ='global name';

{

let name ='ellie';

console.log(name); //ellie

name = 'hello';

console.log(name);//hello

console.log(globalName);

}//괄호 안에 있는것은 전역변수가 아니므로 괄호 안에서만 사용가능

 

 

*var hoisting : 선언하기 전에 변수에 값이 할당되어도 항상 제일 위로 선언을 끌어올림

var은 블럭{} 을 이용해서 안에서만 사용하려고 해도, 밖에서도 사용가능.

->이로인해 let이 사용되기 시작 es6

 

3.constant 상수->immutable type

-security

-thread safety(값변경안하게.)

-reduce human mistakes

 

4.variable types

-primitive, single item: number, string, boolean, null, undefined, symbol

-object, box container

 

//number

const count = 17; //integer

const size = 17.1; //decimal number

console.log(`value: ${count}, type: ${typeof count}`); //17 number

console.log(`value: ${size}, size: ${typeof size}` ); //17.1 number

const infinity = 1/0; //Infinity

const negativeInfinity = -1/0 //-Infinity

const nan ='not a number'/2; //NaN (not a number)

const bigInt =1221213143423423432234242n;

console.log(${typeof bigInt}); //type: bigint

 

//string

const brendan = 'brendan';

const helloBot =`hi ${brendan}!`; //hi brendan! 출력 template literals

혹은 console.log('hi'+brendan+'!');

 

//boolean

//false: 0, null, undefined, NaN, ''

//true: any other value

 

//undefined

let x; or let x = undefined;

 

//symbol, create unique identifiers for objecs

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

console.log(`value: ${symbol1.description}, type: ${typeof symbol1}`); //symbol출력은 .description을 써서 string 으로 변환 후 출력해야함.

 

5. Dynamic typing: dynamically typed language

let text ='7'+5;

console.log(`${typeof text}`);

 

 

6. object

const ellie ={name: 'ellie', age: 20};

ellie.age =30; //이런식으로 바꿀 수 있음.

'개발자 > JS' 카테고리의 다른 글

[자바스크립트] 8.배열  (0) 2021.01.19
[자바스크립트] 7. object  (0) 2021.01.18
6. class vs objec  (0) 2021.01.10
5. Arrow Function 함수의 선언과 표현  (0) 2021.01.04
자바스크립트 4. operator, if, for loop  (0) 2021.01.03