2.7 Type conversion

히진로그·2022년 1월 26일
0

Modern-Javascript

목록 보기
4/14

출처: https://javascript.info/

  • 혼자 읽고 타이핑하며 이해하면서 공부한 흔적 남기기

Most of the time, operators and functions automatically convert the values given to them to the right type.

String Conversion

we can call the String(value) function to convert a value to string.

Numeric Conversion

we can use the Number(value) function to explicitly convert a value to a number.

If the string is not a valid number, the result of such a conversion is NaN.

Numeric conversion rules...

undefined → NaN

null → 0

true and false → 1 and 0

string → Whitespaces from the start and end are removed. If the remaining string is empty, the result is 0. Otherwise, the number is 'read' from the string. An error gives NaN.

Boolean Conversion

The conversion rule:

  • Values that are intuitively 'empty', like 0, an empty string, null, undefined, and NaN, become false.
  • Other values become true.

*in Javascript, a non-empty string is always true.

alert(Boolean(' ')); // true

false ← 0, null, undefined, NaN, ""

true ← any other value, "0"

0개의 댓글