JavaScript Data Types

JavaScript variables can hold different data types: numbers, strings, objects and more.

 

Data types in JavaScript describe the different types or kinds of data that you will be working with and storing in variables.

In Javascript, there are five basic, or primitive, types of data. The five most basic types of data are strings, numbers, booleans, undefined, and null. We refer to these as primitive data types. A single variable can only store a single type of data. That means it’s important for you to learn to store the data correctly.

 

Example:

 

let length = 16; // Number let lastName = "Johnson"; // String let x = {firstName:"John", lastName:"Doe"}; // Object


EXAMPLE ❯


EXAMPLE ❯


JavaScript Types are Dynamic

Javascript is a dynamic typing language. This means that the same variable can be used to hold different data types:

When you declare a variable, you do not need to specify what type this variable is. Javascript engine infers what type this variable is based on the value assigned to at run time... You can change variable c type some where down the road.

EXAMPLE ❯


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:

Strings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their length, to build and concatenate them using the + and += string operators, checking for the existence or location of substrings with the indexOf() method, or extracting substrings with the substring() method.

EXAMPLE ❯


JavaScript Numbers

The Number constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the Number() function.

JavaScript has only one type of numbers.

Numbers can be written with, or without decimals:

A number literal like 37 in JavaScript code is a floating-point value, not an integer. There is no separate integer type in common everyday use.

EXAMPLE ❯


JavaScript Booleans

The Boolean object is an object wrapper for a boolean value.

 

Booleans can only have two values: true or false.

The value passed as the first parameter is converted to a boolean value, if necessary. If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object, an empty array ([]), or the string "false", create an object with an initial value of true.

EXAMPLE ❯


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):

The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects. 

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed.

EXAMPLE ❯


JavaScript Objects

An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects. This chapter describes how to use objects, properties, functions, and methods, and how to create your own objects.

JavaScript objects are written with curly braces {}.

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

EXAMPLE ❯


The typeof Operator

The typeof operator is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand.  The typeof operator evaluates to "number", "string" or "boolean" if its operand is a number, string, or boolean value and returns true or false based on the evaluation.

You can use the JavaScript typeof operator to find the type of a JavaScript variable.

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

EXAMPLE ❯


Undefined

In JavaScript, a variable without a value, has the value undefined. The type is also undefined.

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

EXAMPLE ❯


Empty Values

An empty value has nothing to do with undefined.

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

EXAMPLE ❯