Skip to content

Data types ​

Types in javascript ​

JavaScript has several primitive data types, which are the basic types of data that can be directly operated on. They include:

NameDescriptionExample
StringRepresents textual data"The Great Gatsby"
NumberRepresents both integer and floating-point numbers2023, 19.99
BooleanRepresents a logical entity having two valuestrue, false
UndefinedRepresents a variable that has not been assigned a valueundefined
NullRepresents the intentional absence of any object valuenull
BigIntRepresents integers with arbitrary precision, useful for very large numbers9007199254740992n

Checking the type ​

We can check the type of a variable using the typeof keyword.

js
const pageCount = 210
const ISBN = '978-3-16-148410-0'
const premiumMember = true

console.log(typeof pageCount)
console.log(typeof ISBN)
console.log(typeof premiumMember)
console
number
string
boolean

Adding strings ​

If we have two string types, when we add them, they get concatenated (glued together).

js
let author = 'Toni Morrison'
let title = 'Beloved'

console.log(title + ' was written by ' + author)
console
Beloved was written by Toni Morrison

Changing type ​

In some cases, it is possible to change from one type to another.

js
let bookCount = String(100) // Converts number to string
let price = Number('19.99') // Converts string to number

Type coercion ​

We need to be careful when operating on values of different types.

js
const price1 = '10'
const price2 = 5
const totalPrice = price1 + price2

console.log(totalPrice) // oops!
console
105