0% found this document useful (0 votes)
11 views5 pages

Number Methods

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Number Methods

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

<!-- !

Number methods -->

1. isFinite
2. isInteger
3. isNaN
4. parseInt
5. parseFloat

1. Number.isFinite(value)

- Description:
Determines whether the provided value is a finite number. This method
returns `true` if the value is a finite number, and `false` if it is
`Infinity`, `-Infinity`, or `NaN`.

- Example:

console.log(Number.isFinite(123)); // true
console.log(Number.isFinite(Infinity)); // false
console.log(Number.isFinite('123')); // false
console.log(Number.isFinite(NaN)); // false

2. Number.isInteger(value)

- Description: Determines whether the provided value is an integer. This


method returns `true` if the value is an integer, and `false` if it is not.

- Example:

console.log(Number.isInteger(123)); // true
console.log(Number.isInteger(123.45)); // false
console.log(Number.isInteger('123')); // false
console.log(Number.isInteger(NaN)); // false
3. Number.isNaN(value)

- Description: Determines whether the provided value is `NaN` (Not-a-


Number). This method is a more robust version of the global `isNaN` function
and does not coerce the argument to a number before checking.

- Example:

console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN('NaN')); // false
console.log(Number.isNaN(123)); // false
console.log(Number.isNaN(undefined)); // false

4. parseInt(string)

- Description:
Parses a string argument and returns an integer.

- Example:

console.log(parseInt('123')); // 123
console.log(parseInt('123abc')); // 123 (ignores 'abc')
console.log(parseInt('abc123')); // NaN

5. parseFloat(string)

- Description:
`parseFloat` is a JavaScript function that converts a string into a floating -
point number.

- Examples:

parseFloat('3.14'); // 3.14
parseFloat('3.14abc'); // 3.14 (stops parsing when 'a' is encountered)
parseFloat('abc3.14'); // NaN (not a valid number at the start)
parseFloat(' 42.5 '); // 42.5 (ignores leading and trailing whitespace)
parseFloat('42.5px'); // 42.5 (stops parsing when 'p' is encountered)
let num = Number(10)

console.log(num)
console.log(typeof num)

let num2 = Number("10")

console.log(num2)
console.log(typeof num2) //number

let num3 = Number("10abc")

console.log(num3) //NaN
console.log(typeof num3) //number

// ! Number Methods

// ! 1. Number.parseInt()

let num4 = Number.parseInt("10abc")


console.log(num4)

let num5 = Number.parseInt("a4bc10")


console.log(num5)

console.log('-------------------------------------------')

// ! how to take input from user => prompt() method

let a = prompt("enter one number")


console.log(a)
console.log(typeof a)

// ! take two numbers from users and add

let b = Number.parseInt( prompt("enter first number"))


let c = Number.parseInt(prompt("enter second number"))

alert(b+c)
// ! 2. Number.isFinite()

let num6 = 10000000;

let isFinite = Number.isFinite(num6) // true

console.log(Number.isFinite(2n)) // false (for bigInt)

console.log(isFinite)

console.log(Number.isFinite("hello")) // false (for string)

console.log('-----------------------------------------------')

// ! 3. Number.isInteger()

let num7 = 1234.98

console.log(Number.isInteger(num7)) // false

console.log(Number.isInteger(2012)) // true

console.log("-----------------------------------")

// ! isNaN()

// only if we pass number then only false , otherwise it will give true

console.log(isNaN(123)) // false

console.log(isNaN("san")) // true
console.log(isNaN(NaN)) // true

console.log(isNaN(2n)) // for bigInt it will give error


console.log("----------------------------------------------------")
// ! 4 Number.isNaN()

// only for NaN it will give true, otherwise it will give false

console.log(Number.isNaN(123)) // false
console.log(Number.isNaN('santanu')) // false
console.log(Number.isNaN(NaN)) // true

// ! isNaN() vs Number.isNaN() *****

console.log("----------------------------------------")

// ! 5. Number.parseFloat()

console.log(Number.parseFloat("10.2x3abc")) // 10.2
console.log(Number.parseFloat("ab10.2x3abc")) // NaN

You might also like