JavaScript Data Types

string values, numeric values, boolean values, arrays, objects.

JavaScript Data Types

JavaScript variables can save variousData typesValues such as numbers, string values, arrays, objects, etc.:

var length = 7;                             // Number
var lastName = "Gates";                      // String
var cars = ["Porsche", "Volvo", "BMW"];         // Array
var x = {firstName:"Bill", lastName:"Gates"};    // Object

The concept of data types

Data types are an important concept in the process of programming.

Understanding data types is very important to be able to operate variables.

Without data types, computers cannot safely solve this problem:

var x = 911 + \

Does it make sense to add 911 to "Volvo"? Will it cause an error or will it produce a result?

JavaScript will handle the above example in this way:

var x = "911" + "Porsche";

When a number and a string are added together, JavaScript treats the number as a string.

Example

var x = 911 + \

Try It Yourself

Example

var x = \

Try It Yourself

JavaScript calculates expressions from left to right. Different orders can produce different results:

JavaScript:

var x = 911 + 7 + \

Result:

918Porsche

Try It Yourself

JavaScript:

var x = \

Result:

Porsche9117

Try It Yourself

In the first example, JavaScript treats 911 and 7 as numbers until it encounters \

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

JavaScript has dynamic types

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

Example

var x;               // Now x is undefined
var x = 7;           // Now x is a number
var x = \

Try It Yourself

JavaScript String Values

A string (or text string) is a sequence of characters (for example, \

Strings are enclosed in quotes. You can use either single quotes or double quotes:

Example

var carName = \
var carName = 'Porsche 911';   // Using single quotes

Try It Yourself

You can use quotes inside strings, as long as these quotes do not match the quotes enclosing the string:

Example

var answer = \
var answer = \
var answer = 'He is called \

Try It Yourself

You will learn more about strings in this tutorial.

JavaScript Numbers

JavaScript has only one number type.

Whether to use a decimal point when writing numbers is optional:

Example

var x1 = 34.00;     // With decimal point
var x2 = 34;        // Without decimal point

Try It Yourself

Very large or very small numbers can be written in scientific notation:

Example

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

Try It Yourself

You will learn more about numbers in this tutorial.

JavaScript boolean values

Boolean values have only two values:true or false.

Example

var x = true;
var y = false;

Try It Yourself

Boolean values are often used in conditional tests.

You will learn more about conditional tests in this tutorial.

JavaScript Arrays

JavaScript arrays are written using square brackets.

Array items are separated by commas.

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

Example

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

Try It Yourself

Array indices are based on zero, which means the first item is [0], the second item is [1], and so on.

You will learn more about arrays in this tutorial.

JavaScript Objects

JavaScript objects are written using curly braces.

Object properties are name:value Yes, separated by commas.

Example

var person = {firstName:"Bill", lastName:"Gates", age:62, eyeColor:"blue"};

Try It Yourself

The object (person) in the example has four properties: firstName, lastName, age, and eyeColor.

You will learn more about objects in this tutorial.

typeof operator

You can use JavaScript's typeof To determine the type of a JavaScript variable:

The typeof operator returns the type of a variable or expression:

Example

typeof ""                  // Returns "string"
typeof "Bill"              // Returns "string"
typeof "Bill Gates"          // Returns "string"

Try It Yourself

Example

typeof 0                   // Returns "number"
typeof 314                 // Returns "number"
typeof 3.14                // Returns "number"
typeof (7)                 // Returns "number"
typeof (7 + 8)             // Returns "number"

Try It Yourself

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

Undefined

In JavaScript, a variable without a value has the value of undefinedAlso, typeof returns undefined.

Example

var person;                  // The value is undefined, and the type is undefined.

Try It Yourself

Any variable can be cleared by setting its value to undefined to clear. Its type will also be undefined.

Example

person = undefined;          // The value is undefined, and the type is undefined.

Try It Yourself

Empty value

Empty value and undefined It's not the same thing.

An empty string variable has both a value and a type.

Example

var car = "";                // The value is "", and the type is "string"

Try It Yourself

Null

In JavaScript,null is "nothing". It is considered as a non-existent thing.

Unfortunately, in JavaScript,null has the data type of object.

You can consider null In JavaScript, understanding an object is considered a bug. It should be null.

You can set the value to null Clear an object:

Example

var person = null;           // The value is null, but the type is still object

Try It Yourself

You can also clear an object by setting its value to undefined Clear an object:

Example

var person = undefined;           // The value is undefined, and the type is undefined.

Try It Yourself

The difference between Undefined and Null

Undefined With null are equal in value but not in type:

typeof undefined              // undefined
typeof null                   // object
null === undefined            // false
null == undefined             // true

Try It Yourself

Primitive data

A primitive data value is a single simple data value without any additional properties or methods.

typeof Operators can return one of the following primitive types:

  • string
  • number
  • boolean
  • undefined

Example

typeof "Bill"              // Returns "string"
typeof 3.14                // Returns "number"
typeof true                // Returns "boolean"
typeof false               // Returns "boolean"
typeof x                   // Returns "undefined" (if x has no value)

Try It Yourself

Complex Data

typeof operator can return one of the following two types:

  • function
  • object

typeof operator returns objects, arrays, or null Returns object.

typeof operator does not return functions object.

Example

typeof {name:'Bill', age:62} // Returns "object"
typeof [1,2,3,4]             // Returns "object" (not "array", see the comment below)
typeof null                  // Returns "object"
typeof function myFunc(){}   // Returns "function"

Try It Yourself

typeof operator returns "objectBecause in JavaScript, an array is an object.

Supplementary Reading

Advanced JavaScript Tutorial: