JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Data Types

Data types are used to classify one particular type of data in programming languages.

For instance, a number and a string of characters are different types of data that will be treated differently by JavaScript.

JavaScript variables can hold many data types: numbers, strings, arrays, objects and more:


JavaScript Types are Dynamic

JavaScript is a loosely typed or a dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types.

Run code

JavaScript Numbers

JavaScript has only one number type, there is no separate designation for integers and floating-point numbers.

Because of this, numbers can be written in JavaScript with or without decimals.

Run code

In both cases above, the data type is a number and is the same regardless of whether or not the number has decimal points.

Scientific exponential notation can be used in JavaScript to abbreviate very large or small numbers, as in the following example:

Run code

You will learn more about Numbers later in this tutorial.


JavaScript Strings

A string is a sequence of one or more characters (letters, numbers, symbols). Strings are useful in that they represent textual data.

JavaScript strings are used for storing and manipulating text.

Unlike some other languages, JavaScript makes no distinction between single-quoted strings and double-quoted strings.

Run code

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

Run code

You will learn more about Strings later in this tutorial.


JavaScript Booleans

The Boolean data type can be one of two values, either true or false.

Booleans are often used in conditional testing.

Run code

You will learn more about Booleans later in this tutorial.


JavaScript Arrays

An Array is used to store multiple values in a single variable.

JavaScript arrays are written with square brackets [].

Array items are separated by commas.

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

Run code

Each item or value that is inside of an array is called an element. You can refer to the elements of an array by using an index number.

JavaScript arrays are zero-indexed: the first element of an array is at index 0, the second is 1, and so on.

You will learn more about Arrays later in this tutorial.



JavaScript Objects

In JavaScript, objects can be seen as a collection of properties.

JavaScript objects are written with curly braces {}.

Object properties are written as name:value pairs, separated by commas.

Run code

The object (user) in the example above has 4 properties: firstName, age, color, and location.

You will learn more about Objects later in this tutorial.


The typeof Operator

The typeof operator can help you to find the type of your variable.

The typeof operator returns the type of a variable or an expression.

Run code

Note: The typeof operator returns "object" for arrays because in JavaScript arrays are objects.


Undefined

The undefined value indicates that a variable has not been assigned a value, or not declared at all.

Run code

The type is also undefined.

Run code

Any variable can be emptied, by setting the value to undefined. The type will also be undefined.

Run code

Null

The value null represents the intentional absence of any object value.

In JavaScript, the data type of null is an object.

Any object can be emptied, by setting the value to null.

Run code

You can also empty an object by setting it to undefined.

Run code

Difference between Null and Undefined

null and undefined are equal in value but different in type.

Run code

When checking for null or undefined, beware of the differences between equality (==) and identity (===) operators, as the former performs type-conversion.

Run code

Working with Multiple Data Types

While each program you create will contain multiple data types.

When you use an operator that works across data types, like the + operator that can add numbers or concatenate strings, you may achieve unexpected results.

When adding a number and a string, JavaScript will treat the number as a string.

Run code

When adding a string and a number, JavaScript will treat the number as a string.

Run code

JavaScript evaluates expressions from left to right. Different sequences can produce different results:

Run code
Run code

In the first example, JavaScript treats 5 and 3 as numbers, until it reaches "Sky".

In the second example, since the first operand is a string, all operands are treated as strings.