TIL 11 | JavaScript Data Types

ym j·2021년 3월 15일
0

사전스터디 

목록 보기
11/23
post-thumbnail

자바스크립트의 데이터 타입은 primitive type, object type, function 이 있다.

primitive type(원시 타입 데이터)

  • 더이상 작은 단위로 나뉘지 않는 가장 작은 데이터의 형태이다.

1. number

const num1 = 10;
const size = 1.2;
console.log(num1); // 10
console.log(size): //1.2
const infinity = 1 / 0;
const negativeInfinity = -1 / 0;
const notANumber = 'non' / 1;
console.log(infinity);	Infinity(양의 무한대)
console.log(negativeInfinity); -Infinity(음의 무한대)
console.log(notANumber); //NaN(Not a Number)
  • 숫자형 데이터
  • 숫자형 데이터가 아닌 값이 계산될 경우 NaN 값이 출력된다.


2. string

const char = 'c';
const brendan = 'brendan';
const greeting = 'hello ' + brendan;
console.log(char); // c
console.log(brendan); //brendan
console.log(greeting); // hello brendan (string + 변수)
  • 문자형 데이터
  • ''"" 를 통해 변수에 할당한다.
  • 일반 string과 다른 변수를 +를 통해 또다른 변수에 할당할 수 있다.


3. boolean

const canRead = true; 
const test = 3 < 1; 
console.log(canRead) // true
console.log(test) //false
const name = "Yongmin"
if(name) {
	//실행
}
const number = 100
if(number) {
	//실행 안됨
}
  • true/false값을 반환한다. (해당 조건을 평가 후 참인지 거짓인지 판단)

  • false : 0, null, undefined, NaN(not a number), ''

  • true: false 값을 제외한 모든 값

  • 추가로 자바스크립트에서 if 조건문의 string은 공백을 제외했을 때 모두 true값을 반환하기 때문에 위와 같이 활용할 수 있다.

  • number의 경우, 연산자가 필수이다.



4. null

let nothing = null;
console.log(nothing); // null
  • null 할당은 빈 값이라고 사용자가 직접 지정한 것이다. (빈 껍데기만 있는 느낌?)


5. undefined

let a 
console.log(a) // undefined
  • 아무런 값이 할당되어 있지 않은 상태이다.(null과 달리 껍데기 조차 없음)


object type(객체 타입 데이터)

const yongmin = { name: 'yongmin', age: 29 };
console.log(yongmin.name); // yongmin
console.log(yongmin.age); // 29
  • key, value로 이루어진 객체 타입의 데이터(box-container)도 변수에 할당이 가능하다.


function(함수형 데이터)

const result = function () {
  const a = 10;
  console.log(a);
};
result(); // 10
  • 함수도 다른 데이터 타입처럼 변수에 할당이 가능하다. (first-class function)
  • 이러한 함수 표현 방식을 함수 표현식(function expression)이라고 한다.
profile
블로그를 이전하였습니다 => "https://jymini.tistory.com"

0개의 댓글