Javascript Data Types | Javascript Objects

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 0 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.

Leave a Reply

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