본문 바로가기
JavaScript

Data Type

by 메씨 2023. 4. 24.
728x90
반응형

data type

let name = 'sulki';
console.log(name);//sulki
name = 'hello';
console.log(name);//hello

{let name = 'sulki';
console.log(name);//sulki
name = 'hello';
console.log(name);//hello}

console.log(name);//출력되지 않음

global variable 전역변수는 블록(컬리브라켓 안)에서도 호출되고 밖에서도 호출되나

local 지역변수는 블록 안에서만 가능하다.

전역변수는 최소한으로 설정

let VS var 변수(mutable data type) read & write

  • var은 유연하지만 문법이 붕괴되어 있어 let을 사용하기를 권장

hoisting

어디에 선언을 했는지에 상관없이 문서의 상단으로 끌어올려줌

const 상수(immutable data type) only write

한번 선언하면 다시 선언할 수 없다. (값이 변하지 않는 것을 사용)

const를 사용하는 이유

  • 보안
  • 스레드 안전성
  • 개발자의 실수 줄이기

자료형data type:변수가 저장하는 데이터 primitive VS object

primitive type

value 값을 저장*[immutable]*


  • number
  • string
  • boolean
  • null
  • undefined
  • symbol

Infinity

 const infinity = 1/0;
console.log(infinity);  //Infinity

-Infinity

 const negativeInfinity = -1/0;
console.log(negativeInfinity);  //-Infinity

NaN

 const nAn = 'not a number'/2;
console.log(nAn);  //NaN

dynamic typing

let text = 'hello';
console.log(`value: ${text}, type ${typeof text}`);  //value: hello, type: string
text = 1;
console.log(`value: ${text}, type ${typeof text}`);  //value: 1, type: number
text = '7' + 5;
console.log(`value: ${text}, type ${typeof text}`);  //value: 75, type: string
text = '8' + '2';
console.log(`value: ${text}, type ${typeof text}`);  //value: 4, type: number
728x90
반응형

'JavaScript' 카테고리의 다른 글

Javascript 규칙  (0) 2023.06.14
Operator 연산자  (0) 2023.04.24
Return  (0) 2023.04.24
CSS in JS  (0) 2023.04.24
Event  (0) 2023.04.24

댓글