1. 정수
2. 부동소수점 실수 (float)
3. 부동소수점 실수 (double)
✔️ 부동소수점이란 컴퓨터에서 실수를 표현하는 방법
✔️ IEEE754는 Institute of Electrical and Electronics Enginners (전기 전자 기술자 협회)에서 컴퓨터에 부동소수점을 표현하는 방법을 정의한 것.
우리는 일반적으로 십진법으로 계산하지만 컴퓨터는 이진법 즉 0과 1로만 계산한다. 그래서 컴퓨터가 알아들을 수 있는 0과 1로 구성된 이진법으로 변환해야 하는데, 이때 무한소수로 변환되는 오류가 생길 수 있다.
ex) 0.1 + 0.2 를 했을 때 0.3이 아닌 0.300000000004 로 계산된다.
상수 (constant): 코드에서 변하지 않는 변수
리터럴 (literal): 코드에서 변하지 않는 데이터
const myAge = 20;
-> myAge:상수
-> 20:리터럴
❗️ NaN (Not a Number)의 type도 number type이다.
문자열 "12"
const quantity = "12"; console.log(+quantity);
-> 12
(true)
let num1 = +(true); document.write(num1); document.write(typeof num1);
-> 1
-> number
(false)
let num1 = (false) * 2; document.write(num1); document.write(typeof num1);
-> 0 (0 * 2)
-> number
문자열 "20"
let num1 = ("20") * 2; document.write(num1); document.write(typeof num1);
-> 40
-> number
console.log(Infinity); // Infinity console.log(Infinity + 1); // Infinity console.log(Math.pow(10,1000)); //Infinity (10^1000) console.log(Math.log(0)); //-Infinity (ln(0)) console.log(1 / Infinity); //0 Console.log(1 / 0); // Infinity
❗️ 사용불가
const bigInt = 9007199254740991n
const bigInt = BigInt(9007199254710991);
const bigInt = BigInt(" 9007199254710991 ");
const bigInt = BigInt("0x1fffffffffffff");
const bigInt = BigInt("ob111111111111111111111111111111111111111111111111111111"); -> 9007199254710991n
console.log(Number.isInteger(Infinity)); // false console.log(Number.isInteger('10')); // false console.log(Number.isInteger(true)); // false console.log(Number.isInteger(false)); // false console.log(Number.isInteger([1])); // false
console.log(Number.isInteger(Number(true))); // true --> number타입 변환 1
🔎 문자열 "12.62aaa"는 어떻게 변환될까?
let myAge = "12.62aaa"; document.write(parseFloat(myAge)); // 12.62 document.write(typeof parseFloat(myAge)); // number
🔎 실수값 12.22는 어떻게 변환될까?
let myAge = 12.22; document.write(parseFloat(myAge)); // 12.62 document.write(typeof parseFloat(myAge)); // number
🔎 문자열 "aa 123 aa aa"는 어떻게 변환될까?
let str = "aa 123 aa aa"; document.write(Number.parseFloat(str)); //NaN
--> ❗️시작되는 문자가 숫자형식이 아닌 문자형식이면 실수로 변환하지 못한다.
🔎 문자열 "10 11 12"는 어떻게 변환될까?
let str = "10 11 12"; document.write(Number.parseFloat(str)); // 10
--> ❗️ 맨 앞의 숫자만 변환하고 공백 뒤의 숫자는 버린다.
console.log(Number.isNaN(NaN)); //true console.log(Number.isNaN(Number.NaN)); //true console.log(Number.isNaN(0 / 0)); //true
console.log(Number.isNaN("NaN")); // false console.log(Number.isNaN(undefined)); // false (undefined 타입이다) console.log(Number.isNaN({})); // false console.log(Number.isNaN("blabla")); // false
console.log(Number.isNaN("true")); // false console.log(Number.isNaN("null")); // false (null 타입) console.log(Number.isNaN(37)); // false console.log(Number.isNaN("37")); // false console.log(Number.isNaN("")); // false