bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/JavaScript Foundations
JavaScript•JavaScript Foundations

JavaScript Data Types

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind JavaScript Data Types?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ x = 16 + "Volvo";
3Order

Put the learning moves in the order that makes the concept easiest to apply.

JavaScript Types are Dynamic
The Concept of Data Types
JavaScript Data Types

A JavaScript variable can hold 8 types of data.

7 Primitive Data Types and 1 Object Data Type.

The Object data type can hold many different object types.

TypeDescription
NumberA number representing a numeric value
BigintA number representing a large integer
StringA text of characters enclosed in quotes
BooleanA data type representing true or false
UndefinedA variable with no assigned value
NullA value representing object absence
SymbolA unique primitive identifier
ObjectA collection of key-value pairs of data

The Concept of Data Types

In programming, data types is an important concept.

To be able to operate on variables, it is important to know something about the type.

Without data types, a computer cannot safely solve this:

let x = 16 + "Volvo";

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

JavaScript will treat the example above as:

let x = "16" + "Volvo";

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

Example

let x = 16 + "Volvo";

Example

let x = "Volvo" + 16;

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

JavaScript:

let x = 16 + 4 + "Volvo";

JavaScript:

let x = "Volvo" + 16 + 4;

In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo".

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

JavaScript Types are Dynamic

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

Example

let x;       // Now x is undefined
x = 5;       // Now x is a Number
x = "John";  // Now x is a String

Built-In Object Types

A JavaScript object can represent a JavScript object or a User defined object .

Built-in JavaScript object types can be

ObjectDescription
ArrayArray of values accessed by a numerical index
MapArray of key-value pairs where the keys can be of any data type
SetArray of values where each value can only appear once
WeakMapA type of Map with weak references to the stored objects.
WeakSetA type of Set with weak references to the stored objects.
MathAn object that provides math constants and functions like PI and random()
DateObject for working with dates and times
RegExpObject for working with regular expressions
ErrorObject represents error conditions during program execution
JSONObject with methods for parsing values between JSON and objects
PromiseObject representing the completion or failure of an asynchronous operation
Int8ArrayArray for storing fixed-size 8-bits integer values
Int16ArrayArray for storing fixed-size 16-bits integer values
Int32ArrayArray for storing fixed-size 32-bits integer values
Float16ArrayArray for storing fixed-size 16-bits floating-point values
Float32ArrayArray for storing fixed-size 32-bits floating-point values
Float64ArrayArray for storing fixed-size 64-bits floating-point values
BigInt64ArrayArray for storing fixed-size 64-bits big integer values

The typeof Operator

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:

typeof "" // Returns
"string"
typeof "John" // Returns
"string"
typeof "John Doe" // Returns
"string"
typeof 0 // Returns
"number"
typeof 314 // Returns
"number"
typeof 3.14 // Returns
"number"
typeof (3) // Returns
"number"
typeof (3 + 4) // Returns
"number"

Previous

JavaScript Scope

Next

JavaScript Style Guide