JavaScript

[JavaScript][기초] 데이터 타입

개발새발 2024. 6. 16. 13:18
반응형

 

 

자바스크립트 데이터 타입은 크게 원시(Primitive)타입과 객체(Object)타입으로 구분 된다.

객체 타입은 c/cpp의 포인터나 자바처럼 참조형이기 때문에 주소를 통해 접근된다. (따라서 얕은 복사 주의) 

 

Primitive Type 예)

// Number Type
let num1 = 27;
let num2 = 1.5;
let num3 = -20;

console.log(num1 + num2);
console.log(num1 - num2);
console.log(num1 * num2);
console.log(num1 / num2);
console.log(num1 % num2);

let inf = Infinity;
let mInf = -Infinity;
console.log(inf);
console.log(mInf);

let nan = NaN;

console.log(1 * "hello"); // NaN

// String Type
let myName = "DY";
let myLocation = "Seoul";
let introduceText = `${myName} in ${myLocation}`; // 템플릿 리터럴 문법
console.log(introduceText);

// Boolean Type
let isSwitchOn = true;
let isEmpty = false;

// Null Type (아무것도 없다, 비어있다.)
let emtpy = null;
console.log(emtpy); // null

// Undefined Type (정해지지 않았다.)
let none;
console.log(none); // undefined

 

 

Object Type 예)

// function
function funcA() {
  console.log("test funcA");
}


// Object
const animal = {
  type: "dog",
  name: "kiki",
  color: "black",
};


// Array
let arrTest = [1, 2, 3, 4, 5, true, null, "hello", undefined, () => {}, {}, []];
반응형