Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript data type


May 06, 2021 JavaScript


Table of contents


JavaScript data type


String, Number, Boolean, Array, Object, Null, Undefined.


JavaScript has a dynamic type

JavaScript has a dynamic type. This means that the same variables can be used as different types:

VAR x; // x is undefined
VAR x = 5; // Now x is a number
Var x = "john"; // Now x is a string


JavaScript string

A string is a variable that stores characters, such as "Bill Gates."

Strings can be any text in quotation marks. You can use single or double quotes:

var carname="Volvo XC60";
var carname='Volvo XC60';

You can use quotation marks in strings as long as they do not match the quotation marks that surround the string:

var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';

Try it out . . .

You'll learn more about strings in the advanced part of this tutorial.


JavaScript numbers

JavaScript has only one type of number. Numbers can be taken with or without:

VAR x1 = 34.00; // Write a decimal point
VAR X2 = 34; // Do not write to the decimal point

Large or very small numbers can be written by scientific (exponential) counting:

var y=123e5;      // 12300000
var z=123e-5;     // 0.00123

Try it out . . .

You'll learn more about numbers in the advanced part of this tutorial.


JavaScript Boolean

Boolean (logic) can only have two values: true or false.

var x=true;
var y=false;

Boolean is commonly used in conditional testing. You'll learn more about conditional testing later in this tutorial.


JavaScript array

The following code creates an array named cars:

var cars=new Array();
cars[0]="Saab";
cars[1]="Volvo";
cars[2]="BMW";

Or (the resulted array):

var cars=new Array("Saab","Volvo","BMW");

Or (literal array):

var cars=["Saab","Volvo","BMW"];

Try it out . . .

The array undersrage is zero-based, so the first item is .0, the second is .1, and so on.

You'll learn more about arrays later in this tutorial.


JavaScript object

Objects are separated by parentheses. I nside the parenthesis, the properties of the object are defined as a name and value pair (name: value). The property is separated by a comma:

var person={firstname:"John", lastname:"Doe", id:5566};

The object (person) in the example above has three properties: firstname, lastname, and id.

Spaces and folds don't matter. Declarations can span multiple lines:

var person={
firstname : "John",
lastname  : "Doe",
id        :  5566
};

Object properties are addressed in two ways:

name=person.lastname;
name=person["lastname"];

Try it out . . .

You'll learn more about objects later in this tutorial.


Undefined and Null

The value of Undefined indicates that the variable does not contain a value.

You can empty a variable by setting its value to null.

cars=null;
person=null;

Try it out . . .


Declare the variable type

When you declare a new variable, you can use the keyword "new" to declare its type:

var carname=new String;
var x=      new Number;
var y=      new Boolean;
var cars=   new Array;
var person= new Object;
JavaScript data type JavaScript variables are objects. When you declare a variable, a new object is created.

Tip: JavaScript has an implicit global concept, which means that any variable you don't declare becomes a global object property.

JavaScript Data Type Learning Brain Map:

JavaScript data type


Related articles

JavaScript Standard Reference Tutorial: JavaScript Data Type