Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Data Types

JavaScript Data Types


Value types (primitive types): string (String), number (Number), boolean (Boolean), null (Null), undefined (Undefined), Symbol.

Reference data types (object types): Object, Array, Function, and two special objects: RegExp and Date.

Note:Symbol is a new primitive data type introduced in ES6, representing unique values.


JavaScript has dynamic types

JavaScript has dynamic types. This means the same variable can be used as different types:

Example

var x; // x is undefined

var x = 5; // now x is a number

var x = "John"; // now x is a string

The data type of a variable can be checked with the typeof operator:

Example

typeof

"John"

               

// returns string

typeof

3.14

                 

// returns number

typeof

false

               

// returns boolean

typeof

[

1

,

2

,

3

,

4

]

           

// returns object

typeof

{

name

:

'John'

,

age

:

34

}

// returns object

Try it »

typeof [1,2,3,4] returns "object", which is a "flaw" in the early design of JavaScript; arrays are essentially a special type of object.

The correct way to detect an array:



Array.isArray([1,2,3]); // true

[1,2,3] instanceof Array; // true


JavaScript strings

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

A string can be any text inside quotes. You can use single quotes or double quotes:

Example

var carname="Volvo XC60";

var carname='Volvo XC60';

You can use quotes inside a string, as long as they do not match the quotes surrounding the string:

Example

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 section of this tutorial.


JavaScript numbers

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

Example

var x1=34.00; // written with a decimal point

var x2=34; // written without a decimal point

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

Example

var y=123e5;      // 12300000

var z=123e-5;     // 0.00123

Try it »

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


JavaScript booleans

A boolean (logical) value can have only two values: true or false.

var x=true;

var y=false;

Booleans are often used in conditional tests. You will learn more about conditional tests in later chapters of this tutorial.


JavaScript arrays

The following code creates an array named cars:

var cars=new Array();

cars[0]="Saab";

cars[1]="Volvo";

cars[2]="BMW";

Or (condensed array):

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

Or (literal array):

Example

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

Try it »

Array indexes are zero-based, so the first item is [0], the second is [1], and so on.

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


JavaScript objects

Objects are delimited by curly braces. Inside the braces, an object's properties are defined as name/value pairs (name : value). Properties are separated by commas:

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

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

Spaces and line breaks do not matter. A declaration can span multiple lines:

var person={

firstname : "John",

lastname  : "Doe",

id        :  5566

};

There are two ways to address object properties:

Example

name=person.lastname;

name=person["lastname"];

Try it »

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


Undefined and Null

The value Undefined means a variable has no value.

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

Example

cars=null;

person=null;

Try it »


Declaring variable types

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;

lamp All JavaScript variables are objects. When you declare a variable, a new object is created.