loading

JS Data Types

JavaScript has 8 Datatypes

  • String
  • Number
  • Bigint
  • Boolean
  • Undefined
  • Null
  • Symbol
  • Object

The Object Datatype

The object data type can contain both built-in objects, and user defined objects:

Built-in object types can include:

Objects, arrays, dates, maps, sets, intarrays, floatarrays, promises, and many more.

Examples

				
					// Numbers:
let length = 16;
let weight = 7.5;

// Strings:
let color = "Yellow";
let lastName = "Johnson";

// Booleans
let x = true;
let y = false;

// Object:
const person = {firstName:"John", lastName:"Doe"};

// Array object:
const cars = ["Saab", "Volvo", "BMW"];

// Date object:
const date = new Date("2022-03-25");
				
			

Note

A JavaScript variable can contain any type of data.

The Concept of Data Types

In programming, data types are a crucial topic.

To be able to work with variables, you must first understand their type.

Without data types, a computer cannot safely solve this.

				
					let x = 16 + "Volvo";
				
			

Does it make sense to add “Volvo” to 16? Will it generate an error or produce a result?

JavaScript will interpret the example above as:

				
					let x = "16" + "Volvo";
				
			

Note

When you add a number and a string, JavaScript reads the number as a string.

Example

				
					let x = 16 + "Volvo";
				
			

Example

				
					let x = "Volvo" + 16;
				
			

JavaScript evaluates expressions left to right. Various sequences can provide different outcomes:

JavaScript:

				
					let x = 16 + 4 + "Volvo";
				
			

Result:

				
					20Volvo
				
			

JavaScript:

				
					let x = "Volvo" + 16 + 4;
				
			

Result:

				
					Volvo164
				
			

In the first example, JavaScript considers 16 and 4 as numbers until it encounters “Volvo”.

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

JavaScript Types are Dynamic

Dynamic types are available in JavaScript. This implies that various data types can be stored in the same variable:

Example

---- Example Mukavu -----

JavaScript Strings

A string (or text string) is a collection of characters such as “John Doe”.

Strings are denoted with quotations. You may use single or double quotes:

Example

---- Example Mukavu -----

Quotes can be used inside a string as long as they do not match the quotes surrounding the string.

Example

---- Example Mukavu -----

JavaScript Numbers

All JavaScript integers are represented as decimal numbers (floating points).

Numbers may be written with or without decimals.

Example

---- Example Mukavu -----

Exponential Notation

Scientific (exponential) notation can be used to express extremely big or small numbers:

Example

---- Example Mukavu -----

Note

Most programming languages include a variety of number types:

Integer types include byte (8-bit), short (16-bit), int (32-bit), and long (64-bit).

Real numbers (floating-point) include float (32-bit) and double (64-bit).

Javascript numbers are always one type:
double (64-bit floating point).

This tutorial will teach you more about numbers in the future.

JavaScript BigInt

Every number in JavaScript is kept in a 64-bit floating-point representation.

A new datatype (ES2020) called JavaScript BigInt can be used to store integer values that are too large to fit into a standard JavaScript Number.

Example

---- Example Mukavu -----

JavaScript Booleans

----- text lakhvu ----

Example

---- Example Mukavu -----

Booleans are commonly used in conditional testing.

JavaScript Arrays

JavaScript arrays are denoted by square brackets.

The array entries are separated by commas.

The code below declares (creates) an array called cars, which contains three entries (vehicle names):

Example

---- Example Mukavu -----

Array indexes are zero-based, therefore the first item is [0], then [1], and so on.

JavaScript Objects

JavaScript objects are denoted by curly braces {}.

Commas are used to divide name:value pairs, which represent object properties.

Example

---- Example Mukavu -----

FirstName, lastName, age, and eyeColor are the four characteristics of the object (person) in the example above.

You’ll learn more about objects later in this session.

The typeof Operator

---- text mukavu ---

Example

---- Example Mukavu -----

Example

---- Example Mukavu -----

Later in this course, typeof will be covered in greater detail.

Undefined

---- text mukavu ---

Example

---- Example Mukavu -----

---- text mukavu ---

Example

---- Example Mukavu -----

Empty Values

---- text mukavu ---

Example

---- Example Mukavu -----

Share this Doc

JS Data Types

Or copy link

Explore Topic