ReasonJun

javascript : built-in object (number, math, url) 본문

Frontend/Javasciprt

javascript : built-in object (number, math, url)

ReasonJun 2023. 6. 7. 01:10
728x90

number

//? .toFixed()
// Returns a number as a character representing a number to the specified decimal notation (number of digits).

console.log(num.toFixed(2)); // 3.14
console.log(parseFloat(num.toFixed(2))); // 3.14 => number

// .toLocalString()
// Returns a number as a character in local language format.

const num1 = 100000;
console.log(num1.toLocaleString()); // 100,000 => string

// Number.isInteger()
// Checks if a number is an integer.

const num2 = 123;
const pi = 3.14;

console.log(Number.isInteger(num2)); // true
console.log(Number.isInteger(pi)); // false

// Number.isNaN()
// Checks if the given value is 'NaN'.

const num3 = NaN;
const num4 = 123;
const str = 'hello';
const nul = null;

console.log(Number.isNaN(num3)); // true
console.log(Number.isNaN(num4)); // false
console.log(Number.isNaN(str)); // false
console.log(Number.isNaN(nul)); // false

//? Number.parseInt() or parseInt()
// Parses the given value (number, character) and returns it as an integer of a specific radix.

const str1 = '3.141592';
const num5 = 3.141592;

console.log(parseInt(str1, 10)); // 3
console.log(parseInt(num, 10)); // 3

//? .Number.parseFloat() or parseFloat()
// Parses the given value (number, character) and returns it as a floating point number.

console.log(parseFloat(str1)); // 3.141592
console.log(parseFloat(num5)); // 3.141592


//? .toExponential()
// Exponential notation (used to express very large or small numbers, expressed as 10 to the nth power)
const num3 = 102;
console.log(num3.toExponential()); // 1.02e+2


//? .toPrecision()
// Round to valid number of digits
console.log(num4.toPrecision(5)); // 1234.1
console.log(num4.toPrecision(4)); // 1234
console.log(num4.toPrecision(2)); // Exponential notation when precision is not possible 1.2e+3


//? .EPSILON
if (Number.EPSILON > 0 && Number.EPSILON < 1) {
  console.log(Number.EPSILON); // The smallest number that can be represented between 0 and 1 => 2.220446049250313e-16
}

function isEqual(original, expected) {
  return Math.abs(original - expected) < Number.EPSILON;
} 
console.log(isEqual(1, 1)); // true
console.log(isEqual(0.1, 0.1)); // true
console.log(isEqual(num, 0.1)); // false

math 

//? Math.abs()
// Returns the absolute value of a given number.

console.log(Math.abs(2)); // 2
console.log(Math.abs(-2)); // 2

//? Math.ceil()
// Rounds the given number up and returns an integer.

console.log(Math.ceil(3.14)); // 4

//? Math.floor()
// Rounds the given number down to return an integer.

console.log(Math.floor(3.14)); // 3

//? Math.max()
// Returns the largest of the given numbers.

console.log(Math.max(1, 22, 88)); // 88

//? Math.min()

console.log(Math.min(1, 22, 88)); // 1

//? Math.pow()

console.log(Math.pow(4, 2)); // 16
console.log(4 ** 2); // 16

//? Math.random()
// Returns a random number greater than or equal to 0 and less than 1.

console.log(Math.random()); // 0.6153861937561977

// Function to get a random integer in a specific range
function random(min = 0, max = 10) {
  return Math.floor(Math.random() * (max - min)) + min;
}

console.log(random()); // 4
console.log(random(11, 20)); // 13
console.log(random(101, 999)); // 173

//? Math.round()
// Returns an integer by rounding the given number.

const num = 3.14;
const num1 = 3.76;

console.log(Math.round(num)); // 3
console.log(Math.round(num1)); // 4



//? eval
eval('const num = 2; console.log(num)'); // 2

//? isFinite
console.log(isFinite(1)); // true
console.log(isFinite(Infinity)); // false

//? Math
console.log(Math.E); // Euler's constant, the base of the natural logarithm
console.log(Math.PI); // pi value

// square root
console.log(Math.sqrt(9));

// return only integers
console.log(Math.trunc(1.5432));

url

When there are Korean characters or special characters, it must consist of only ASCII characters to transmit the address.

const URL = '<https://안녕.com>';
const encoded = encodeURI(URL);
console.log(encoded);
//https://%EC%95%88%EB%85%95.com

const decoded = decodeURI(encoded);
console.log(decoded);
//https://안녕.com

// Use components for partial URLs rather than full URLs
const part = '안녕.com';
console.log(encodeURIComponent(part));
//%EC%95%88%EB%85%95.com

 

728x90
Comments