Skip to content

Split View: Javascript data 타입 정리

|

Javascript data 타입 정리

var, let, const 비교

javascript에서 변수를 선언하는 방법은 3가지이다. var 키워드의 경우 function scope로 선언했을 때 함수 내부에서 모두 사용이 가능한 반면에, letconst의 경우 block scope로 블록 내에서만 사용할 수 있는 변수가 된다. letconst에도 차이점이 있는데, let의 경우 해당 변수가 다른 값으로 재할당 될 수 있지만, const의 경우 재할당이 불가능하다. immutable의 여부라고 생각해도 된다.

자바스크립트 타입

Java script 에서 제공하는 데이터 타입은 여러가지가 있지만 크게 2가지로 나눌 수 있다. 첫번 째는 원시타입(Primitive type)이고 두번째는 참조타입(Reference type)이다.

  • 원시타입 : Boolean, String, Number, null, undefined, Symbol(es6)
  • 참조타입 : Object, Array, function, classes

원시타입의 경우 값을 저장하기 위해 Call Stack 을 사용하지만, 참조타입은 Heap 메모리를 사용한다.

참조타입의 경우 데이터의 크기가 정해지지 않고, 데이터의 값이 heap에 저장되며, 변수에 Heap메모리의 주소값이 할당됩니다.

자바스크립트는 동적(dynamic)타입의 언어이기 때문에 runtime에 변경될 수 있는 여지가 있다. 따라서 변수 정의 때, 자료형을 명시하지 않아도 된다.

example
const a = "asdf"; //string
const b = 123; // number
let c = 'foo';
c = 123123; // dynamic type
c = true;
c = Symbol(); // symbol type
c = ["hi", "hello"];
console.log(typeof c);

Summary of Javascript Data Types

Comparing var, let, and const

There are three ways to declare variables in JavaScript. The var keyword has function scope, meaning the variable is accessible throughout the entire function when declared. On the other hand, let and const have block scope, meaning the variable is only accessible within the block where it was declared. There is also a difference between let and const: let allows the variable to be reassigned to a different value, while const does not allow reassignment. You can think of it in terms of whether the variable is immutable or not.

JavaScript Types

There are several data types provided in JavaScript, but they can be broadly divided into two categories. The first is Primitive types, and the second is Reference types.

  • Primitive types: Boolean, String, Number, null, undefined, Symbol (ES6)
  • Reference types: Object, Array, function, classes

Primitive types use the Call Stack to store values, while Reference types use Heap memory.

For Reference types, the size of the data is not fixed, the data values are stored in the heap, and the variable is assigned the address of the Heap memory.

Since JavaScript is a dynamically typed language, values can be changed at runtime. Therefore, you do not need to specify a data type when defining a variable.

example
const a = "asdf"; //string
const b = 123; // number
let c = 'foo';
c = 123123; // dynamic type
c = true;
c = Symbol(); // symbol type
c = ["hi", "hello"];
console.log(typeof c);

Quiz

Q1: What is the main topic covered in "Summary of Javascript Data Types"? Summary of Javascript data types

Q2: What are the key takeaways from this article? Summary of Javascript data types

Q3: How can the concepts in this article be applied in practice? Consider the practical examples and patterns discussed throughout the post.