JS/JS 공부

[JS] dataType 데이터 타입, 자료형

congs 2023. 4. 12. 11:35

dataType

: 변수나 상수에 할당할 수 있는 값의 종류

 

Number 숫자

  • 정수, 실수, NaN(not a number: 숫자가 아님 이라는 값)
let num = 1234;

console.log(`num: ${typeof num}`); 
// 출력 ) num: number

console.log(`nan: ${typeof NaN}`);
// 출력 ) nan: number

 

문자 string

  • " ", ' ', ` ` 으로 감싼 값들
let str = "문자";

console.log(`str: ${typeof str}`);
// 출력 ) str : string

 

불리언 Boolean

  • true / false
 let bool = true;
 
console.log(`bool: ${typeof bool}`);
// 출력 ) bool: boolean

 

객체 Object

  • 1개 이상의 데이터가 특정한 구조를 가지고 있는 형태
  • 아무것도 없는 사실도 1개로 취급함
  • { }
  • 객체안에 배열이 들어갈 수 없음
let obj2 = { };
let obj1 = new Object();   // new Object : 자바스크립트에서 제공하는 내장객체       

console.log(`obj1: ${typeof obj1}`);       
console.log(`obj2: ${typeof obj2}`);
// 출력) obj1: object
// 출력) obj2: object

 

배열 Array

  • [1, 2, 3, ...] 형태로 이루어진 집합
  • 순서, 길이 있음
  • 배열안에 객체가 들어갈 수 있음
  • 같은 자료의 값을 담고 있는 객체 (타 자료가 들어간다면 객체로 규정)
let arr = [1, 2, 3];       
let arr2 = new Array(); // 자바스크립트에서 제공하는 Array 객체

 console.log(`arr: ${typeof arr}`);       
console.log(`arr2: ${typeof arr2}`);
// 출력 ) arr: object
// 출력 ) arr2: object

 

알수없는 값 Null

  • 뭔가 있지만 파악이 안되는 값
let nul = null;

console.log(`nul: ${typeof nul}`);
// 출력 ) nul: object

 

미정된 값 undefined

  • 값이 할당되지 않은 값
let undi = undefined; 
let nudi2;

console.log(`undi: ${typeof undi}`);
console.log(`undi2: ${typeof undi2}`);

// 출력 ) undi: undefined
// 출력 ) undi2: undefined

 

+ console.log(typeof 변수명) ;

: 데이터의 타입을 알고 싶은 경우

 


변수의 자료형

 

기본 자료형 Primative Type

  • 값이 메모리의 주소값이 가르키는 곳에 직접할당
  • number, boolean, string (원래는 참조 자료형이지만 사용 편의성을 위해 사용 가능)

 

잠조 자료형 Reference Type

  • 값이 저장되어 있는 메모리 주소값을 잠초하고 있는 할당
  • string, object, array, function, null, undefined

 


비교

== 값이 같다 / === 값의 자료형까지 같다
!= 값이 다르다 / 값과 자료형도 같지 않다
<script>

let num1 = 10;
let num2 = '10';

console.log('num1 == num2', num1 == num2);  // true 반환
console.log('num1 === num2', num1 === num2); // false 반환
        
</script>

 

'JS > JS 공부' 카테고리의 다른 글

[JS] return 리턴  (0) 2023.04.12
[JS] function 함수  (0) 2023.04.12
[JS] console.log(변수명) ; 콘솔 출력  (0) 2023.04.12
[JS] 백틱 ` `  (0) 2023.04.12
[JS] 변수 선언 및 사용  (0) 2023.04.12