ReasonJun

Javascript : (2) (string, number) 본문

Frontend/Javasciprt

Javascript : (2) (string, number)

ReasonJun 2023. 6. 6. 13:19
728x90
const string1 = 'Hello';
const string2 = 'Hello';
const string3 = `Hello ${string1}`;

console.log(string3); // Hello Hello

const number = -123;

console.log(number + 1); // -122
console.log(number + undefined); // NAN
console.log(typeof (number + 1)); // -122
console.log(typeof (number + undefined)); // number

const a = 0.1;
const b = 0.2;
console.log(a + b); // 0.30000000000000004
// Floating Point Error: An infinite decimal occurred while converting a decimal number to a binary number.
console.log((a + b).toFixed(1)); // 0.3 => string
console.log(typeof (a + b).toFixed(1)); // string
console.log(typeof Number((a + b).toFixed(1))); // number




// bigint

// bigint is an integer of unlimited length.
// Numeric data can be reliably displayed,
// Integers greater than the maximum value of {2^53 - 1} can be expressed.
// Add 'n' to the end of the integer or call 'BigInt()' to create it.

console.log(12345678901234567890123456789012345678901234567890); // 1.2345678901234567e+49

console.log(12345678901234567890123456789012345678901234567890n); // 12345678901234567890123456789012345678901234567890n
console.log(BigInt('12345678901234567890123456789012345678901234567890')); // 12345678901234567890123456789012345678901234567890n

const a = 11n;
const b = 22;

console.log(a + BigInt(b)); // 33n
console.log(typeof (a + BigInt(b))); // bigint

console.log(Number(a) + b); // 33
console.log(typeof (Number(a) + b)); // number

 

728x90
Comments