Data Types in JavaScript

Data Types in JavaScript

A variable can hold a lot of types of data type like string, int, bool etc in JavaScript.

Syntax:

var <variable_name> = <value> ;

var is a key word in JavaScript to define any type of variable.

Example:

As we see from above example name is string and age is a number but both are defined using the same keyword var. So what if we try to concatenate both of them.

Now A =usman20 because while joining string with numbers JavaScript use numbers as strings.

Now what if we add two numbers like:

Is it 2010 no in that case both values are numbers so it will add them and the result is A=30.

But what if we have

Now Javascript considers all the three values as a string because they are starting with a string. And it produces the result A = usman2010.

When we execute the above line then it produces the result as A=30usman. It’s because first two values are numbers so it first add them as a number and then third value is a string so it convert the sum of first two number into a string and concatenate it with string.

JS Dynamic Data Types:

This means we save different types of value in same variable and they all initialize using var variable.

Strings in JavaScript:

In JavaScript string variable are declare as all with var keyword and we write string in double quote or single quote.

Numbers in JavaScript:

Numbers are also defined using var keyword.

Boolean in JavaScript:

As Boolean numbers have only two values true or false.

Arrays in JavaScript:

Arrays are defined using two methods in javascript.

Literal array and condenser array.

Another method is:

2D – Arrays in JavaScript:

2D arrays are array within array in javascript like:

Now this is assessing as following: