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

4 comments :

  1. 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
  2. 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
  3. The people are very lucky to have this blog because it has better knowledge.
    Bay Area design firm

    ReplyDelete
  4. const {readFileSync, promises: fsPromises} = require('fs');

    // ✅ read file SYNCHRONOUSLY
    //https://bobbyhadz.com/blog/javascript-read-file-into-array#:~:text=Use%20the%20fs.,get%20an%20array%20of%20strings.
    function syncReadFile(filename) {
    const contents = readFileSync(filename, 'utf-8');

    const arr = contents.split(/\r?\n/);

    //console.log(arr); // 👉️ ['One', 'Two', 'Three', 'Four']

    return arr;
    }

    let availableF = syncReadFile('./AvailableFileds_Prod_Oct19.txt');
    let availableSet = cleanStringCreateSet(availableF);
    console.log("Total Fields Available: " + availableSet.size);

    let missingF = syncReadFile('./missing_fields_jira.txt');
    let missingSet = cleanStringCreateSet(missingF);
    console.log("Total Fields in Missing: " + missingSet.size);

    for (const item of availableSet) {
    //console.log(item);
    }

    for (const item of missingSet) {
    //console.log(item);
    }

    let presentCnt = 0;
    let absetCnt = 0;


    for (const item of missingSet) {
    if(availableSet.has(item)) {
    //console.log("present");
    presentCnt++;
    }
    else {
    console.log(item);
    absetCnt++;
    }
    }


    console.log("Total Present " + presentCnt);
    console.log("Total Absent " + absetCnt);

    function cleanStringCreateSet(availableF) {
    let availableSet = new Set();
    for (const item of availableF) {
    //https://flaviocopes.com/how-to-remove-last-char-string-js/
    let entry = item.slice(0, -1); //delete last character (,)
    entry = entry.slice(1); //delete first character (")
    entry = entry.slice(0, -1) //delete last character (")
    //console.log(entry);
    availableSet.add(entry)
    }
    return availableSet;

    }

    ReplyDelete

Please leave your message queries or suggetions.