Loading...
JavaScript

Data Types in JavaScript

What is Data Type?

In programming the term Data Type refers to type or classification of data that a variable can hold and operations that can be performed on it. It hints the interpreter or compiler how the data is intended to be used by programmer.

In JavaScript, as per latest ECMAScript standard there are nine different Data Types which can be grouped into three broad categories –

  1. Primitive or Primary – undefined, Boolean, Number, String, BigInt and Symbol
  2. Non-data Structure or Composite – Object and Function
  3. Special – null

Let’s understand the Data Types in detail.

undefined:

A variable that has not been assigned a value has the value undefined

Boolean:

Boolean Data Type represents logical entity that holds two values – true and false

Number:

A Number type variable can have values: (numbers between -(253 − 1) and 253 − 1). In addition to representing floating-point numbers, the number type has three symbolic values: +Infinity-Infinity, and NaN (Not a Number).

String:

It is used to represent textual data.

BigInt:

It is a numeric primitive which can have integer values larger than the range supported by the Number data type with arbitrary precision. BigInt make it possible to correctly perform integer arithmetic without overflowing issues. A BigInt is created by appending n to the end of an integer or by calling the constructor.

1234567890123456789n * 12345n; // Result: 15240740603574074060205n

Symbol:

A Symbol is a unique and immutable primitive value and may be used as an identifier for object properties. Symbol variables are created by using Symbol() with optional String argument as it’s description (without new keyword). Symbol() returns unique value every time it is called.

let sym1 = Symbol()
let sym2 = Symbol('foo')
let sym3 = Symbol('foo')

let sym4 = new Symbol(); // Throws TypeError
Symbol('foo') === Symbol('foo')  // false

null:

null is a primitive data type that has only single value null. Null means nothing or something that doesn’t exist.

Primitive Data Types can hold only a single value whereas Composite Data Types can hold multiple values.

var name = "Sam"; // String
var age = 25; // Number
var isDeveloper = false; // Boolean

// Function
var a;
alert(a); // Output : undefined

var a = null;
alert(a); // Output: null

// Object
var obj = {
   "name": "Tom", 
   "age": 25,
   "isDeveloper": false
} 

Difference between null and undefined:

It is important to note that undefined and null are two distinct types – undefined is a type itself (undefined) while null is an object. Using the typeof operator you can easily determine type of a variable. Checkout the following examples:

var x;
alert(x); //shows undefined
alert(typeof x); //shows undefined  

var x = null;
alert(x); //shows null
alert(typeof x); //shows Object

In JavaScript Array belongs to Object Data Type:

var fruits = ["Apple", "Orange", "Banana"];
alert(typeof fruits); //shows Object

Now, here are few JS coding interview questions for you. What will be the output of the following code in JavaScript?

null === undefined
null == undefined
null === null
+0 === -0
25 / +0
25 / -0 

Post your answers in the comment box below.

Dynamic Typing in JavaScript:

Unlike other programming languages, JavaScript is a loosely typed and dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types:

var foo = "bar"; // foo is string
foo = 15; // foo is now a number
foo = false; // foo is now a boolean

var x; // variable x can hold any value

JavaScript is strange, isn’t it? In the upcoming articles, I will cover many more interesting topics. Stay tuned.

You can try all above examples here: Datatype Code Editor

Share this article with your friends
Leave a Reply

Your email address will not be published. Required fields are marked *