Data Types In JavaScript

Programming languages all have erected-in data types, but these frequently differ from one language to another. This post attempts to list the erected-in data structures available in JavaScript and what properties they have.

In this post, you will learn about the various data types available in JavaScript with the help of examples.

JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.

Primitive data type.
Non-primitive (reference) data type.

 

Primitive data type.

The JavaScript language provides 6 primitive data types:

Undefined
Null
Number
String
Boolean
Symbol

Undefined:

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

var language; //variable declared but not defined.
console.log(language); // Output: undefined

Null:

A variable has been defined with a null value. It is like as undefined. It’s store nothing.

You can assign null to a variable to denote that currently, that variable does not have any value but it will have later on. A null means absence of a value.

var language = null; //It holds null value
var lang; // It holds undefined value

console.log(language == lang); // it return true, value is same of language and lang but data type different
console.log(language === lang); // it is return false, value is same of language and lang but data type different

Number:

There is only one number type. There is no specific type for integers. The number is a primitive data type used for positive or negative integer, float, binary, octal, hexadecimal, and exponential values in JavaScript

String:

A string in JavaScript is a sequence of characters. It is textual content. It can be created with the help of single or double-quotes.

var str1 = “Hello, World!”;
var str2 = ‘Hi, Welcome to JavaScript Tutorial’;

Boolean:

It is logical a entity. A variable can have two values true or false.

var isActive = true;
var isDisabled = false;

Symbol:

New in ECMAScript6. A Symbol is a unique and immutable identifier.

var x = Symbol();
var y = Symbol(10);
var z = Symbol(‘Hello’);

 

2. Non-primitive (reference) data type.

Non-primitive data types are called reference types because they refer to objects. The ‘object’ is a non-primitive data type in JavaScript. Arrays and Functions in JavaScript belong to the ‘object’ data type.

Object:

An object in JavaScript contains key-value pairs in its address. When we refer to obj1, we are actually referring to the address in memory which contains the value {a: 5, b: 6}, instead of the value {a: 5, b: 6} directly.

var obj1 = { a: 5, b: 6 };

We can change or mutate the value of obj1.

obj1[a] =7;
console.log(obj1) // will return the value {a: 7, b: 6}

Array:

With the help of an array, we can store more than one element under a single name.

// Call it with no arguments
var a = new Array();

// Call it with single numeric argument
var b = new Array(10);

// Explicitly specify two or
// more array elements
var d = new Array(1, 2, 3, “Hello”);

Find All Date Between Two Dates In JavaScript.              Different Ways To Remove An Elements From An Array In JavaScript.

The post Data Types In JavaScript appeared first on PHPFOREVER.

Leave a Reply

Your email address will not be published.