JavaScript Data Types

Value types (basic types) : String, Number, Boolean, Null, Undefined, Symbol.

Reference data types : Object, Array, Function.

Note: Symbol is a new primitive data type introduced by ES6, which represents a unique value.

JavaScript Has Dynamic Types

JavaScript has dynamic types. This means that the same variable 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

Strings are variables that store characters (such as "Bill Gates").

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


var carname="Dodge Caliber";
var carname='Dodge Caliber';

You can use quotation marks in the string as long as it does not match the quotation marks surrounding the string:


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

 Try It! 

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

JavaScript Numbers

JavaScript has only one type of number. Numbers can be with or without a decimal point:


var x1=34.00; //use decimal point to write
var x2=34; //do not use decimal point to write

Very large or very small numbers can be written by scientific (exponential) notation:


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

 Try It! 

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

JavaScript Boolean

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

var x=true;
var y=false;

Boolean is often used in conditional testing. You will learn more about conditional testing in later chapters of this tutorial.

JavaScript Array

The following code creates an array named cars:

var cars=new Array();

Or (condensed array):

var cars=new Array("Subaru","Dodge","BMW");

Or (literal array):


var cars=["Subaru","Dodge","BMW"];

 Try It! 

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

You will learn more about arrays later in this tutorial.

JavaScript Object

Objects are separated by curly braces. Inside the brackets, the properties of the object are defined in the form of name and value pairs (name:value). The attributes are separated by commas:

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

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

Spaces and line breaks are irrelevant. The statement can span multiple lines:

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

There are two addressing methods for object properties:



 Try It! 

You will learn more about objects in later chapters of this tutorial.

Undefined and Null

The value Undefined means that the variable does not contain a value.

You can clear the variable by setting the value of the variable to null.



 Try It! 

Declare 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;
lampJavaScript variables are all objects. When you declare a variable, you create a new object.