An Overview of Primitive Types

This is what MDN Web Docs states on the topic of Primitive Types:

“In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods. There are 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null.”

Primitive values are values that cannot be changed, or in other words, primitive values are immutable. A variable can be assigned a primitive value and in that case, the value in the variable can be reassigned but the value cannot be changed, like in arrays, functions and objects.

Let’s review the 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null.

STRING

A String is an object that contains a series of characters that can be changed and represent an object.

The MDN Web Docs says this about the String primitives and String objects

“String primitives and String objects also give different results when using eval(). Primitives passed to eval are treated as source code; String objects are treated as all other objects are, by returning the object.”

Here are some examples of a string primitive and a string object:

let blue5 = ’11 + 22' // creates a string primitive

let orange7 = new String(’25 + 21') // creates a String object

console.log(eval(blue5)) // returns the number 33

console.log(eval(orange7)) // returns the string “25 + 21”

NUMBER

The MDN Web Docs says this about the Number:

“Number is a primitive wrapper object used to represent and manipulate numbers like 37 or -9.25.

The Number constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the Number() function.

The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#. This means it can represent fractional values, but there are some limits to what it can store. A Number only keeps about 17 decimal places of precision; arithmetic is subject to rounding. The largest value a Number can hold is about 1.8E308. Numbers beyond that are replaced with the special Number constant Infinity.

A number literal like 37 in JavaScript code is a floating-point value, not an integer. There is no separate integer type in common everyday use. (JavaScript now has a BigInt type, but it was not designed to replace Number for everyday uses. 37 is still a Number, not a BigInt.)”

“Number.prototype.valueOf()

Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf() method.”

The MDN Web Docs says this about Number.prototype.valueOf(),

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

Here are examples of the valueOf() method:

const numObj = new Number(42);

console.log(typeof numObj);

// expected output: “object”

const num = numObj.valueOf();

console.log(num);

// expected output: 42

console.log(typeof num);

// expected output: “number”

BIGINT

For number values greater than 2⁵³ — 1, BigInt is used.

The MDN Web Docs says this about BigInt:

“BigInt is a built-in object whose constructor returns a bigint primitive — also called a BigInt value, or sometimes just a BigInt — to represent whole numbers larger than 2⁵³ — 1 (Number.MAX_SAFE_INTEGER), which is the largest number JavaScript can represent with a number primitive (or Number value). BigInt values can be used for arbitrarily large integers.

A BigInt value, also sometimes just called a BigInt, is a bigint primitive, created by appending n to the end of an integer literal, or by calling the BigInt() constructor (but without the new operator) and giving it an integer value or string value.

const previouslyMaxSafeInteger = 9007199254740991n

const alsoHuge = BigInt(9007199254740991)

// ↪ 9007199254740991n

const hugeString = BigInt(“9007199254740991”)

// ↪ 9007199254740991n

const hugeHex = BigInt(“0x1fffffffffffff”)

// ↪ 9007199254740991n

const hugeOctal = BigInt(“0o377777777777777777”)

// ↪ 9007199254740991n

const hugeBin = BigInt(“0b11111111111111111111111111111111111111111111111111111”)

// ↪ 9007199254740991n

I quiet don’t understand this yet, but I’m sure with more experience I’ll understand it soon.

“When tested against typeof, a BigInt value (bigint primitive) will give “bigint”:

typeof 1n === ‘bigint’ // true

typeof BigInt(‘1’) === ‘bigint’ // true

A BigInt value can also be wrapped in an Object:

typeof Object(1n) === ‘object’ // true

BOOLEAN

The MDN Web Docs says this about Boolean:

The Boolean object is an object wrapper for a boolean value.

The value passed as the first parameter is converted to a boolean value, if necessary. If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (“”), the object has an initial value of false. All other values, including any object, an empty array ([]), or the string “false”, create an object with an initial value of true.

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

Any object of which the value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:

var x = new Boolean(false);

if (x) {

// this code is executed

}

SYMBOL

The MDN Web Docs says this about Symbol:

Symbol is a built-in object whose constructor returns a symbol primitive — also called a Symbol value or just a Symbol — that’s guaranteed to be unique. Symbols are often used to add unique property keys to an object that won’t collide with keys any other code might add to the object, and which are hidden from any mechanisms other code will typically use to access the object. That enables a form of weak encapsulation, or a weak form of information hiding.

Every Symbol() call is guaranteed to return a unique Symbol. Every Symbol.for(“key”) call will always return the same Symbol for a given value of “key”. When Symbol.for(“key”) is called, if a Symbol with the given key can be found in the global Symbol registry, that Symbol is returned. Otherwise, a new Symbol is created, added to the global Symbol registry under the given key, and returned.

To create a new primitive Symbol, you write Symbol() with an optional string as its description:

let sym1 = Symbol()

let sym2 = Symbol(‘foo’)

let sym3 = Symbol(‘foo’)

The above code creates three new Symbols. Note that Symbol(“foo”) does not coerce the string “foo” into a Symbol. It creates a new Symbol each time:

Symbol(‘foo’) === Symbol(‘foo’) // false

The following syntax with the new operator will throw a TypeError:

let sym = new Symbol() // TypeError

This prevents authors from creating an explicit Symbol wrapper object instead of a new Symbol value and might be surprising as creating explicit wrapper objects around primitive data types is generally possible (for example, new Boolean, new String and new Number).

If you really want to create a Symbol wrapper object, you can use the Object() function:

let sym = Symbol(‘foo’)

typeof sym // “symbol”

let symObj = Object(sym)

typeof symObj // “object”

UNDEFINED

The MDN Web Docs says this about Undefined:

undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.

Example

var x; //create a variable but assign it no value

console.log(“x’s value is”, x) //logs “x’s value is undefined”

NULL

The MDN Web Docs says this about Null:

In computer science, a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address. The meaning of a null reference varies among language implementations.

In JavaScript, null is marked as one of the primitive values, because its behavior is seemingly primitive.

But in certain cases, null is not as “primitive” as it first seems! Every Object is derived from null value, and therefore typeof operator returns object for it:

typeof null === ‘object’ // true

This has been An Overview of Primitive Types.

Hello, I am a young enthusiastic Software Engineer. Looking forward to an opportunity to use my skills for mutual benefits and growth as well.