Skip to content

Number.valueOf()

Why valueOf ?

The valueOf() method returns the wrapped primitive value of a Number object.

Syntax:

number.valueOf()

A Number, representing the primitive value of a number.

What is Primitive Value?

  • Primitive values are the type of values that a variable can hold. Primitive value is stored directly in the location that the variable accesses. Primitive values are data that are stored on the stack.
  • Primitive types include Undefined, Null, Boolean, Number, or String.

JavaScript valueOf Examples

Example 1

        let number = 20;
        let valOf = number.valueOf();
        console.log(valOf)

output:

20


Example 2


        let number = new Number(25.469);
        console.log("JavaScript number.valueOf() is " + number.valueOf());

output:

JavaScript number.valueOf() is 25.469


Example 3

        let myObject = new Number(1453);
        console.log(typeof myObject);

        let number = myObject.valueOf();
        console.log(number);
        console.log(typeof number)

output:

object
1453
number


Example 4

        let myObject = new Number(-1453);
        console.log(typeof myObject);

        let number = myObject.valueOf();
        console.log(number);
        console.log(typeof number)

output:

object
-1453
number


Example 5

        let myObject = new String("this is my world");
        console.log(typeof myObject);

        let number = myObject.valueOf();
        console.log(number);
        console.log(typeof number)

output:

object
this is my world
number


sources:


Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: