JavaScript 변수와 자료형

윤로그·2021년 9월 3일

JavaScript

목록 보기
2/9
post-thumbnail

변수(variable) 선언 방식

1. let

  • 선언 후 값이 변경될 수 있음 (mutable)
  • ES 6부터 추가
  • 메모리 값 읽고(read) 쓰기(write) 가능
let os = 'mac';
console.log(os); // mac

os = 'linux';
console.log(os); // linux

2. const

  • 선언 후 값이 변경될 수 없음 (immutable)
  • 값이 변되어야 할 이유가 없다면 let 보다는 const를 사용

    security
    thread safety
    reduce human mistakes

const os = 'windows';
console.log(os); // windows

3. var

  • 선언하기도 전에 값 할당 가능
  • 값을 할당하기도 전에 출력 (undefined) 가능
  • block scope 적용되지 않음
os = 'mac';
console.log(os); // mac 

var hoisting

  • 선언 위치와 상관없이 맨 위로 선언 됨

자료 타입(data type)

1. number

const Num1 = 2030; // 정수(intege
const Num2 = 20.5; // 소수(decimal)
const Infinity = 1 / 0; // 숫자를 0으로 나누게 되면 인피니티
const Negativeinfinity = -1 / 0; // 마이너스 값을 0으로 나누게 되면 네거티브 인피니티
const NaN = '1' / 2; // 숫자가 아닌 문자를 0으로 나누게 되면 NaN(not a number)

2. string

const Char = 'c';
const first_Name = '윤';
const last_Name = '승근';
const Name = first_Name + last_Name // + 기호를 이용 한 문자열 합치기
console.log(Name) // 윤승근
console.log('10' + 1000); // 101000 문자열에 숫자를 더하게 되면 숫자가 문자로 변환  

3. boolean

  • false : 0 null undefined NaN
  • true : any other vlaue
const people = true; // 변수에 할당 가능
const Test = 20 > 50 // false 

4. null

  • null 값을 할당해 줘야 함
let nothing = null;

5. undefined

  • 선언은 했지만 값이 할당되지 않은 상태
let A;
let A = undefined

5. symbol

  • 고유한 식별자가 필요하거나 동시다발적으로 일어나는 작업에서 우선순위를 주고 싶을 시 사용
const symbol1 = Symbol('id');
const symbol2 = Symbol('id');
console.log(symbol1 === symbol2) // false

0개의 댓글