TIL 04 | 형 변환 (Type conversion)

CHAEIN·2021년 6월 15일
0

JavaScript

목록 보기
4/31

String(), Number(), boolean()

console.log(10 + 5) // 15
console.log('10' + '5') // 105

console.log(String(10) + String(5)) // 105
console.log(number('10') number('5')) // 15

1. String()

숫자 -> 문자

let x = 123
console.log(x); // 123
console.log(String(x)); // '123'
console.log(type of x); //  number
console.log(type of String(x)); // string

불린 -> 문자

let y = true;
console.log(y); // true
console.log(String(y)); // 'true'
console.log(type of y); //  boolean
console.log(type of String(y)); // string

2. Number()

문자 -> 숫자

let x = '123';
console.log(x); // '123'
console.log(Number(x)); // 123
console.log(type of x); //  string
console.log(type of String(x)); // number

불린 -> 숫자

let y = true;
console.log(y); // true
console.log(Number(y)); // 1 , false 는 0
console.log(type of y); //  boolean
console.log(type of Number(y)); // number

3. Boolean()

문자 -> 불린

let x = '문자';
console.log(x); // '문자'
console.log(Boolean(x)); // true
console.log(type of x); //  string
console.log(type of Boolean(x)); // boolean

숫자 -> 불린

let y = 123;
console.log(y); // 123
console.log(Boolean(y)); // true
console.log(type of y); //  number
console.log(type of Boolean(y)); // boolean

Truthy와 Falsy 값

boolean 값으로 참이 나오는 데이터 타입을 Truthy, 거짓이 나오는 데이터 타입을 Falsy 라고 한다.

  • Truthy(참 같은 값) : true, {}, [], 1, 2, 'false' -12, '3.14' ...
  • Falsy(거짓 같은 값) : flase, '', null, undefined, 0, -0, NaN(not a number)

0개의 댓글

관련 채용 정보