Sitemap

Get a better understanding of Primitives in JavaScript

3 min readApr 22, 2024

--

Press enter or click to view image in full size

Primitive Type in JavaScript is straightforward to understand but beginers often get confused because of its features and dynamic nature. In this article, I have explained the Primitive Types in JavaScript.

Datatypes in JavaScript

There are two types of datatypes that JavaScript provides. They are as follows:

  • Primitive Types
  • Referenced Types

This article is all about Primitive Types in JavaScript.

Primitives in JavaScript

Primitives are the basic datatype that a language supports. The following are the 7 Primitives available in JavaScript:

  • string
  • numbers
  • boolean
  • bigint
  • symbol
  • null
  • undefined

Nature of Primitives in JavaScript

  • Primitives in JavaSctipt are not objects.
  • Primitives do not have any properties or methods of their own.
  • Primitive values are stored in Stack Memory.

Immutability

The values that are stored in primitives cannot be changed. Hence whenever we perform any modification on these variables it doesn't change the value instead it creates a new value.

Get Ayush Gondaliya’s stories in your inbox

Join Medium for free to get updates from this writer.

Wrapper Classes and Autoboxing

Primitive types don't come with properties and methods because they are mutable. Hence JavaScript provides us with Autoboxing. Autoboxing is a concept in JavaScript where primitive types are temporarily converted to their corresponding object wrappers when certain methods or properties are accessed on them. This happens when the primitive type value is used in the context that requires its wrapper object such as calling a method. This allows us to use various properties and methods on the primitives. JavaScript provides wrapper classes for all primitives except null and undefined (i.e., String, Boolean, Number, Symbol, and BigInt).

let a = 'abc';
console.log(a.toUpperCase()); //ABC

In the above example, a is a primitive string type, which does not have any methods or properties of its own but when we invoke a method of String Class i.e.,toUpperCase() method on it, JavaScript makes it possible with Autoboxing.

Type Coercion

Type Coercion is the implicit conversion of a value from one type to another. It happens in two different cases:

  • When we try to operate on two different types of variables, JavaScript will perform Type Coercion, rather than returning an error.
let a = 123;
let b = '45';
console.log('a + b =', a+b); //12345

In the above code, the number variable a is converted to a string and is concatenated with string value b

  • When a variable is assigned to a different type.
let a = 20;
console.log(a, typeof a); //20 number
a = '20';
console.log(a, typeof a); //20 string

In the above example, a is of number type but when a string is assigned to it, Type Coercion takes place.

The behavior of Primitives with Equals ( = )and Strict Equals ( == ) Operator

  • When we use equals for checking two different values. JavaScript will perform Type Coercion on one of the operands, which may result in bugs and errors.
console.log('123' == 123); //true
console.log(1 == true); //true
console.log('' == false); //true
  • Hence, using the Strict Equals operator to compare two different values is best. This will prevent Type Coercion and will return the correct output.
console.log('123' === 123); //false
console.log(1 === true); //false
console.log('' === false); //false

null and undefined in JavaScript

  • undefined is the value that is assigned to a variable after its declaration till its initialization. Whereas, null is the special value that represents the international absence of the value of the particular variable.
  • undefined is the primitive type and the property of the global object in JavaScript. Whereas, null is considered as an object in JavaScript. This doesn't mean that null has any of its properties or methods. It simply means that null refers to a variable in the memory that has no value.
  • null and undefined both are considered falsy values in Javascript.
console.log(!!null);       //false
console.log(!!undefined); //false

Thanks for Reading

  • Please consider Clapping and Following the witter.
  • Connect with me on LinkedIn

--

--

Ayush Gondaliya
Ayush Gondaliya
0 followers

I use this channel to share my learning journey in programming, JavaScript and related fields. "Spreading knowledge is just as important as acquiring it."