Skip to content

4.5 Data Types Part 2

Number Object Constructor

Task 1: Assign 163 to myVar. Display the type and the value of myVar in the console.

var myVar = 163;

console.log(`The value is: ${myVar}`);
console.log(`The type is: ${typeof myVar}`);

The result is as follows:

The value is: 163
The type is: number

Numeric Literals

A numeric literal is a sequence of digits.

In task 1; 163 is a numeric literal.

Task 2: Assign new Number("479") to myVar. Display the type and the value of myVar in the console.

var myVar = new Number("479");

console.log(`The value is: ${myVar}`);
console.log(`The type is: ${typeof myVar}`);

The result is as follows:

The value is: 479
The type is: object

Task 3: Refer to task 2; Convert myVar value to a string using toString().

var myVar = new Number("479");

var newVar = myVar.toString();
console.log(`The value is: ${newVar}`);
console.log(`The type is: ${typeof newVar}`);

The result is as follows:

The value is: 479
The type is: string

The Number Object

The number object is a regular number with associative properties and methods.

Primitive Number <==> Number Object

When you access a number property or method, JavaScript converts this number to its respective number object.

The respective number object has number properties and methods.

Then JavaScript converts the number back to its primitive value.

Hence, a number does not have properties or methods, however, its respective object does have.

Task 4: Refer to task 2; myVar value is an object, how was it constructed? Hint: use myVar.constructor.

var myVar = new Number("479");

console.log(`${myVar.constructor}`);

The result is: function Number() { [native code] }

Number() constructor

Number() is a constructor function that creates new number objects.

To create a number using Number() constructor, use the keyword new followed by Number(yourNumber).

Task 5: Create a new number using Number() constructor. This number value is -58

var myNumber = new Number(-58);

console.log(`${myNumber}`);

The result is: -58

Task 6: Refer to task 5; What is the type of myNumber value?

var myNumber = new Number(-58);

console.log(`The type is: ${typeof myNumber}`);

The result is: The type is: object

Task 7: Refer to task 5; Which type of primitive data does myNumber correspond to?

var myNumber = new Number(-58);

console.log(`The primitive type is: ${typeof myNumber.valueOf()}`);

The result is: The primitive type is: number

Task 8: Create a new number using Number() constructor. This number value is 88.192. Display the number value, its type, and its corresponding primitive value.

var myNumber = new Number(88.192);

console.log(`The value is: ${myNumber}`);
console.log(`The type is: ${typeof myNumber}`);
console.log(`The primitive type is: ${typeof myNumber.valueOf()}`);

The result is as follows:

The value is: 88.192
The type is: object
The primitive type is: number

Task 9: Create a new number using Number() constructor. This number value is -0.0001. Display the number value, its type, and its corresponding primitive value.

var myNumber = new Number(-0.0001);

console.log(`The value is: ${myNumber}`);
console.log(`The type is: ${typeof myNumber}`);
console.log(`The primitive type is: ${typeof myNumber.valueOf()}`);

The result is as follows:

The value is: -0.001
The type is: object
The primitive type is: number

Boolean Object Constructor

Task 10: Assign true to myVar. Display the type and the value of myVar in the console.

var myVar = true;

console.log(`The value is: ${myVar}`);
console.log(`The type is: ${typeof myVar}`);

The result is as follows:

The value is: true
The type is: boolean

Boolean Literals

A Boolean literal has two values: true and false.

Task 11: Assign new Boolean(false) to myVar. Display the type and the value of myVar in the console.

var myVar = new Boolean(false);

console.log(`The value is: ${myVar}`);
console.log(`The type is: ${typeof myVar}`);

The result is as follows:

The value is: false
The type is: object

Task 12: Refer to task 11; Convert myVar type to a string using toString().

var myVar = new Boolean(false);

var newVar = myVar.toString();

console.log(`The value is: ${newVar}`);
console.log(`The type is: ${typeof newVar}`);

The result is as follows:

The value is: false
The type is: string

The Boolean Object

The Boolean object is a regular Boolean with associative properties and methods.

Primitive Boolean <==> Boolean Object

When you access a Boolean property or method, JavaScript converts this Boolean value to its respective Boolean object.

The respective Boolean object has Boolean properties and methods.

Then, JavaScript converts the Boolean back to its primitive value.

Hence, a Boolean does not have properties or methods, however, its respective object does have.

Task 13: Refer to task 11; myVar value is an object, how was it constructed? Hint: use myVar.constructor.

var myVar = new Boolean(false);

console.log(`${myVar.constructor}`);

The result is: function Boolean() { [native code] }

Boolean() constructor

Boolean() is a constructor function that creates new Boolean objects.

To create a Boolean using Boolean() constructor, use the keyword new followed by Boolean(bool).

Task 14: Create a new Boolean using the Boolean() constructor. This Boolean value is true. Display the Boolean value, its type, and its corresponding primitive value.

var myBool = new Boolean(true);

console.log(`The value is: ${myBool}`);
console.log(`The type is: ${typeof myBool}`);
console.log(`The primitive type is: ${typeof myBool.valueOf()}`);

The result is as follows:

The value is: true
The type is: object
The primitive type is: boolean