Java Script Data Types

JavaScript Data Types

This article covers the datatypes available for JavaScript. The latest ECMAScript standard defines nine types: 7 Primitive types and 2 Structural Types.

Primitive Types

The six primitive types are Boolean, Null, Undefined, String, Number, BigInt, and Symbol.

Boolean type

Boolean represents a logical entity and can have two values: true and false. The Boolean object will be with the initial true only if the parameter passed is an object, array, or true. Else the Boolean object will be false.
Following code shows the different values the Boolean object will have when passed different parameters.
var b1 = new Boolean(); 
console.log(b1); //false

var b2 = new Boolean(false); 
console.log(b2);//false

var b3 = new Boolean(0); 
console.log(b3);//false

var b4 = new Boolean(NaN); 
console.log(b4);//false

var b5 = new Boolean(undefined); 
console.log(b5);//false

var b6 = new Boolean(null);
console.log(b6); //false

var b7 = new Boolean({}); 
console.log(b7);//true

var b8 = new Boolean(true); 
console.log(b8);//true

var b9 = new Boolean([]); 
console.log(b9);//true

Null type

The Null type has exactly one value: null. Every Object is derived from null value, and therefore typeof operator returns object for it.
The following code shows one important feature of null, it is of type object.
var n1 = null; 
console.log(n1); //null
console.log(typeof n1); //object

Undefined type

A variable that has not been assigned a value has the value undefined.
The following code shows one example where a variable u1 is defined but not assigned any value.
var u1 ;
console.log(typeof u1);//undefined
console.log(u1);//undefined

String

The string type is used to represent textual data. JavaScript strings are immutable. This means that once a string is created, it is not possible to modify it
Strings can be created as primitives, from string literals, or as objects, using the String() constructor. In case of primitive typeof returns "string" otherwise it returns "object". String object can be converted to string primitives using valueOf() method on the String object.
const string1 = "A string primitive";
const string2 = new String("A String object");
console.log(typeof string1); //string
console.log(typeof string2); //object
console.log(typeof string2.valueOf());//string 

Number

The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(2^53 − 1) and 2^53 − 1). In addition to representing floating-point numbers, the number type has three symbolic values: +Infinity, -Infinity, and NaN ("Not a Number"). Number.MAX_VALUE or Number.MIN_VALUE represents the maximum and minimum values possible using numbers.
console.log(Number.MAX_VALUE); //1.7976931348623157e+308
console.log(Number.MIN_VALUE); //5e-324
console.log(1/0); //Infinity

BigInt type

The BigInt type is a numeric primitive in JavaScript that can represent integers with arbitrary precision. A BigInt is created by appending n to the end of an integer or by calling the constructor. BigInts cannot be operated on interchangeably with Numbers. Instead, a TypeError will be thrown.
Following code shows how to find the value of 2^53, we have choosen 53 because then the product value will be beyond the capacity of numbers. If you
const xbig = 2n ** 53n;
console.log(xbig); //9007199254740992n

Symbol type

A Symbol is a unique and immutable primitive value and may be used as the key of an Object property (see below).
The following example shows how to create a Symbol object.
let sym1 = Symbol()
let sym2 = Symbol('foo')
let sym3 = Symbol('foo')

Structural Types

Object and Functions are two structural data types.
typeof operator shows that for object and function the operator returns "object" and "function" respectively.
var o1 = {}; 
console.log(typeof o1);//object

var f1 = function () {}; 
console.log(typeof f1);//fuction

3 comments :

  1. For the excellent company Begin with the internet site, the customer service over the cell phone, to the detailed care they deliver in your presence
    leather jacket

    ReplyDelete
  2. Either the drive can be conveyed to our office situated in Bangalore or it very well may be gotten and dropped to customer's and client's area (Currently accessible assistance in Lab recuperation in Bangalore area) or individual visit to our office area, other city or nation is upheld by means of dispatch office.hdd data recovery service

    ReplyDelete
  3. As a company, we want to get ahead of the curve, and they gave us the opportunity to see how to do that
    UX teams

    ReplyDelete

Please leave your message queries or suggetions.