parseInt()
vs parseFloat()
parseInt()
는 정수를 반환하고 parseFloat()
는 부동 소수점 숫자를 반환한다.
parseInt()
는 소수일 경우 소숫점을 없애고 정수를 표현한다.
console.log(parseInt("60.123"); // 60
console.log(parseFloat("60.123"); // 60.123
만약 parseInt()에 문자가 들어가면 어떻게 될까?
parseInt("hello")
와 같이 문자만 들어갈 경우 NaN
의 결과를 가진다.
그럼 문자와 숫자를 같이 사용하는 경우에는 어떻게 될까? parseInt("10abc")
일 경우에 abc
는 없어지고 10
만 출력하게 된다. 하지만 parseInt("abc10")
인 경우에는 결과가 NaN
이 나온다.
이를 통해 알 수 있는 것은 숫자로 먼저 시작할 경우 문자가 있어도 숫자만 출력하지만 문자로 시작할 경우에는 NaN을 출력한다는 것을 알 수 있다. 공백인 경우에도 NaN을 출력한다.
parseFloat()도 같은 규칙을 가진다.
console.log(parseInt(" ")) // NaN
console.log(parseInt("")) // NaN
console.log(parseInt("hello"); //NaN
console.log(parseInt("abc10"); //NaN
console.log(parseInt("10abc"); //10
toFixed()
toFixed()
는 소수점 아래 몇 번째까지만 값을 가질 것인지 표현하는 메서드이다. 사용법은 선언한 값에 .toFixed(표현하고자 하는 소수점 아래 자릿수)
로 표현하면 된다.
const x = 20.345123;
console.log(x.toFixed(1)); //20.3
console.log(x.toFixed(2)); //20.35 (반올림)
Math의 메서드 및 상수
Math.abs()
: 절댓값 반환
Math.ceil()
: 소수점 첫째 자리에서 올림
Math.floor()
: 소수점 첫째 자리에서 내림
Math.round()
: 소수점 첫째 자리에서 반올림
Math.sqrt()
: 제곱근값 반환
Math.PI
: 원주율 상수
Math.min()
: 입력된 값 중 가장 작은 값
Math.max()
: 입력된 값 중 가장 큰 값
Math.random()
: 0 이상 ~ 1 미만 값에서 난수를 반환
Math.pow()
: 거듭제곱근
Math.abs(-20.321); // 20.321
Math.ceil(20.321); // 21
Math.floor(20.321); // 20
Math.round(20.521); // 21
Math.round(20.321); // 20
Math.sqrt(4) // square root, 루트 // 2
Math.PI // 3.141592653589793
Math.min(3, 1, 6, 20, 60, 4); // 1
Math.max(3, 1, 6, 20, 60, 4); // 60
parseInt(Math.random() * 10 + 1) // 1부터 10까지 난수 생성
Math.pow(2, 3) // 2 ** 3
JavaScript에서는 내부 표현 방식으로 인해 (253-1)(9007199254740991) 보다 큰 값 혹은 -(253-1) 보다 작은 정수는 숫자형
으로 나타낼 수 없다. 이를 보안하기 위해 사용하는 것이 BigInt
형이다. 리터럴 끝에 n을 붙이면 된다.
// 표현이 안되는 경우
9007199254740991 + 1 // 9007199254740992
9007199254740991 + 2 // 9007199254740992
// BigInt형
9007199254740991n + 1n // 9007199254740992n
9007199254740991n + 2n // 9007199254740993n
BigInt(9007199254740991) + BigInt(1) // 9007199254740992n
BigInt(9007199254740991) + BigInt(2) // 9007199254740993n
Boolean형으로 변환 시 적용되는 규칙
숫자 0, 빈 문자열, null, undefined, NaN
과 같이 직관적으로도 비어있다고 느껴지는 값들은 false가 된다.console.log(Boolean('hello')) // t
console.log(Boolean(' ')) // f
console.log(Boolean([1, 2, 3])) // t
console.log(Boolean([])) // t
console.log(Boolean({'one' : 10, 'two' : 20})) // t
console.log(Boolean({})) // t
console.log(Boolean(0)) // f
console.log(Boolean(-1)) // t
console.log(Boolean(undefined)) // f
console.log(Boolean(null)) // f
console.log(Boolean(NaN)) // f
논리연산자(&&
,||
)를 사용하여 연산할 때 값이 좌측 값에 따라 우측 값이 결정된다.
let username = "";
username = username || "무명";
console.log(username); //무명
let username2 = " "; // 공백
username2 = username2 || "무명";
console.log(username2); // //빈 값 출력
예제에는 ||
, 합의 값으로 둘 중 하나라도 true면 true이다.
username에는 어떤 값도 없다(undefined). undefined는 false이기 때문에 뒤에 값을 확인한 다음 "무명"이라는 값이 있기 때문에 "무명" 값을 반환한다. username2는 공백이 있다. 공백도 값으로 처리하기 때문에 unsername2는 true이므로 뒤에 값은 확인하지 않는다.
console.log(NaN == NaN); // false
console.log(NaN === NaN); // false
console.log(isNaN(undefined)); // true
console.log(isNaN(null)); // false
console.log(isNaN(NaN)); // true
// ES6에서 추가 도입
console.log(Number.isNaN(undefined)); // false
console.log(Number.isNaN(null)); // false
console.log(Number.isNaN(NaN)); // true