javascript

Javascript Data Types | Javascript Objects


Data types and objects are the basic keywords that are required by any language. Javascript holds a variety of data types that are easy to use.

 

Loosely Typed Language

Javascript provides dynamic data types (it is loosely typed language), it means we don’t need to define the type (whether it is integer or string), whatever we assign at the time of assignment it accepts its type as its data type.

Example:

var a = 5

Here we have assigned an integer, so it will take an integer as its data type.

 

Dynamic Typing

 In Javascript, we can also explicitly change the data type of a variable. 

Example – :

var a = 5;

a = “String”

Here at first, we have assigned an integer in variable ‘a’ so it takes an integer as its data type. But in the next line, we assign a string value. This does not give an error in javascript, we can explicitly change the data type of variable.

 

Javascript has some own rules for converting the data type implicitly. 

 

Example -: 

var a = “String” + 5   // ‘+’ symbol is used for concatenation in javascript

console.log(a)          // prints “String5”

Here Javascript implicitly converts the data type to string even there is an integer involved.

 

These are some of the basics of how javascript data types work.

 

Let’s move into the different Javascript data types.

 

Javascript Data types

There are basically two types of data types Primitives and Objects. Primitives data types are the types that define the type of a variable and has no methods, unlike objects.

 

The primitive Javascript data types are -:

  1. Boolean 
  2. Number
  3. String
  4. Null 
  5. Undefined

 

Boolean:

The boolean value can be true or false and or 1. If we assign any of these values to a variable it is defined as boolean.

 

var a = true

var b = false

 

Number:

Any number integer or a float value comes into the data type number. The numbers can also be +Infinity, -Infinity and Nan (not a number). It is a double-precision 64-bit binary format number, which means the value lies between ((2^(-53))-1  – (2^53) -1) that occupies the whole 64 bit in a system.

 

var a = +Infinity

 

String:

A string is a group of characters generally text, it can store any type of character. The Strings are initialized within double quotes (“”).

 

var a = “This is a String 12345”

 

Null:

Null is a data type which means nothing. Null is assigned to a variable when the variable is empty. 

var a = null

 

Undefined:

The value of an empty variable is Undefined.

var a

console.log(a)     //undefined

 

Javascript Objects:

An Object holds a memory denoted by an identifier that contains different types of properties. Properties are the key-value pair, keys can be a string or symbol and values can be of any type. Values can be string, integer or even the javascript objects itself, which helps in handling complex data. The Object is not a primitive javascript data types so, it comes with different inbuilt methods.

Example -:

var a = {

num : 0,

key : “String”

}

 

Here, a is an object that has keys num’ and key’The values associated with these keys are 0′ and String’ respectively.

 

Functions as Objects:

When a function is defined it inherits the built-in properties because of its prototype. These are the various properties associated with a function that makes our work easier.  

Example -:

        function myFunc (param1, param2, param3){

//Body of a function

        }

        console.log(myFunc.length)      // returns the number of parameter the function holds

 

Similarly, there are properties as well that are associated with a function.

 

Example:-  .call(), .apply() etc.

 

Arrays as Objects:

Arrays defined can be treated as an object since arrays inherit a bunch of inbuilt properties from its prototype. Arrays inherit the methods or properties from Array.prototype that helps to manipulate the array. ECMAScript has provided us with a lot of functionalities that can be implemented by treating the array as an object.

 

Example -:  

array.map((value, index) => { return //body  })

array.forEach((value, index) => { return //body  })

array.filter((value, index) => { return //body  })

array.length

array.slice()

array.splice()

array.push()

array.pop()

and many more …

 

Map and Set:

Till now, we have learned how to store complex data

  1. Arrays – that uses ordered collection of data that can be accessed by indexes.
  2. Objects – it uses keys to store the collection of data

 

But, these are not enough to deal with real-life scenarios, that is why maps and sets were introduced.

 

Maps:

Maps store the key-value pair same as object, but the difference is, maps can store any type of keys whereas objects were able to store only string keys.

 

Methos and Properties of maps are -:

new map() – It will create a new map

map.set(key, value) – It stores the key-value pair

map.get(key) – It gets the value associated key in a map

map.has(key) – Checks whether the key is defined if it does not then return undefined

map.delete(key) – It removes the key-value pair associated with the key.

map.clear() – It removes all key-value pairs from the map

map.size() – returns the count of the key-value pair

map.keys(), map.values() and map.entries are used for the iteration of the map object.

 

Example -:

let map = new Map();

 

map.set(‘1’, ‘str1’);

map.set(1, num1);

map.set(true, ‘bool1’);

 

Sets:

Set is a set of values without keys. Set is used to store a value only once if we store the same value repeatedly, it will do nothing.

Sets can be iterated with the help of forEach and inbuilt functions as set.keys(), set.values() and set.entries.

 

The methods of the set are -:

set.add(value) – adds a value to a set

set.delete(value) – delete a value from a set

set.has() – checks if the value is present in the set, returns true or false

set.clear() – removes all the values from a set

Set.size – return the count of values in a set

 

Example -:

let set = new Set();

let jhon = { name : “Jhon”};

let pete = { name : “Pete” };

let mary = { name : “Mary” };

 

set.add(jhon);

set.add(pete);

set.add(mary);

 

JSON Object:

Suppose there is a complex object or an array that we want to send over a network. We know that data transferred through the network is in the form of bytes, so we will not be able to transfer data in one go. JSON objects enable us to send the data through a network in the form of a string. Using JSON methods we can convert any array or object into the JSON object and can send over a network, on the receiving end we can again convert it back to array or object by using JSON methods. 

 

Example-:

Suppose we have a object

var a = {

name : “name”,

company_name : “company_name”,

address : “address”

}

 

We can convert this object into the JSON object by using JSON.stringify() method.

var json = JSON.stringify(a);

This JSON object is then sent over a network.

At the receiving end again we have to convert it back to the object then we use JSON.parse()

var obj = JSON.parse(json);

 

This is all one has to learn about Javascript Data Types and Javascript Objects.


Be it a software developer, programmer, coder, or a consultant, CronJ has it all. CronJ has been a trustworthy company for startups, small companies, and large enterprises. Hire the web of experienced ReactJS Development Services for your esteemed project today.
 

Hope you enjoyed learning it.

Thank you!!

Write a Comment