Easy Tutorial
❮ Js Comparisons Jsref Split ❯

JavaScript Data Types


Value Types (Primitive Types): String, Number, Boolean, Null, 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 a unique value.


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;           // x is now a number
var x = "John";      // x is now a string

The data type of a variable can be checked using 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

JavaScript Strings

Strings are variables that store characters (e.g., "Bill Gates").

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

Example

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

You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

Example

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

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


JavaScript Numbers

JavaScript has only one type of number. Numbers can be written with, or without, decimals:

Example

var x1 = 34.00;      // written with decimals
var x2 = 34;         // written without decimals

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

Example

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

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


JavaScript Booleans

Booleans can only have two values: true or false.

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


JavaScript Arrays

The following code creates an array called cars:

Or (condensed array):

Or (literal array):

Example

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

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 enclosed in curly braces. Inside the braces, the object's properties are defined as name-value pairs (name : value). Properties are separated by commas:

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

Whitespace and line breaks are not important. A declaration can span multiple lines:

Object properties can be accessed in two ways:

Example

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

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


Undefined and Null

Undefined indicates that a variable has no value.

A variable can be emptied by setting its value to null.

Example

cars = null;
person = null;

Declaring Variable Types

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

| | JavaScript variables are all objects. When you declare a variable, you create a new object. | | --- | --- |

❮ Js Comparisons Jsref Split ❯