JavaScript Types are Dynamic


JavaScript has dynamic types. This means that the same variable can be used to hold different data types:


Example

var x;           // Now x is undefined

x = 5;           // Now x is a Number

x = "John";      // Now x is a String


JavaScript Strings

A string (or a text string) is a series of characters like "John Doe".

Strings are written with quotes. You can use single or double quotes:


Example

var carName1 = "Volvo XC60";   // Using double quotes

var carName2 = 'Volvo XC60';   // Using single quotes


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


Example

var answer1 = "It's alright";             // Single quote inside double quotes

var answer2 = "He is called 'Johnny'";    // Single quotes inside double quotes

var answer3 = 'He is called "Johnny"';    // Double quotes inside single quotes


You will learn more about strings later in this tutorial.


JavaScript Numbers

JavaScript has only one type of numbers.

Numbers can be written with, or without decimals:


Example

var x1 = 34.00;     // Written with decimals

var x2 = 34;        // Written without decimals


Extra large or extra small numbers can be written with scientific (exponential) notation:


Example

var y = 123e5;      // 12300000

var z = 123e-5;     // 0.00123

Try it yourself »

You will learn more about numbers later in this tutorial.


JavaScript Booleans

Booleans can only have two values: true or false.


Example

var x = 5;

var y = 5;

var z = 6;

(x == y)       // Returns true

(x == z)       // Returns false


Booleans are often used in conditional testing.

You will learn more about conditional testing later in this tutorial.


JavaScript Arrays

JavaScript arrays are written with square brackets.

Array items are separated by commas.

The following code declares (creates) an array called cars, containing three items (car names):


Example

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

Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya