Skip to content

JavaScript Types

New Course Coming Soon:

Get Really Good at Git

You might sometimes read that JS is untyped, but that's incorrect. It's true that you can assign all sorts of different types to a variable, but JavaScript has types. In particular, it provides primitive types, and object types.

Primitive types

Primitive types are

And two special types:

Let’s see them in detail in the next sections.

Numbers

Internally, JavaScript has just one type for numbers: every number is a float.

A numeric literal is a number represented in the source code, and depending on how it’s written, it can be an integer literal or a floating point literal.

Integers:

10
5354576767321
0xCC //hex

Floats:

3.14
.1234
5.2e4 //5.2 * 10^4

Strings

A string type is a sequence of characters. It’s defined in the source code as a string literal, which is enclosed in quotes or double quotes

'A string'
"Another string"

Strings can span across multiple lines by using the backslash

"A \
string"

A string can contain escape sequences that can be interpreted when the string is printed, like \n to create a new line. The backslash is also useful when you need to enter for example a quote in a string enclosed in quotes, to prevent the char to be interpreted as a closing quote:

'I\'m a developer'

Strings can be joined using the + operator:

"A " + "string"

Template literals

Introduced in ES2015, template literals are string literals that allow a more powerful way to define strings.

const a_string = `something`

You can perform string substitution, embedding the result of any JS expression:

`a string with ${something}`
`a string with ${something+somethingElse}`
`a string with ${obj.something()}`

You can have multiline strings easily:

`a string
with
${something}`

Booleans

JavaScript defines two reserved words for booleans: true and false. Many comparision operations == === < > (and so on) return either one or the other.

if, while statements and other control structures use booleans to determine the flow of the program.

They don’t just accept true or false, but also accept truthy and falsy values.

Falsy values, values interpreted as false, are

0
-0
NaN
undefined
null
'' //empty string

All the rest is considered a truthy value.

null

null is a special value that indicates the absence of a value.

It’s a common concept in other languages as well, can be known as nil or None in Python for example.

undefined

undefined indicates that a variable has not been initialized and the value is absent.

It’s commonly returned by functions with no return value. When a function accepts a parameter but that’s not set by the caller, it’s undefined.

To detect if a value is undefined, you use the construct:

typeof variable === 'undefined'

Object types

Anything that’s not a primitive type is an object type.

Object types have properties and also have methods that can act on those properties.

How to find the type of a variable

Any variable has a type assigned. Use the typeof operator to get a string representation of a type:

typeof 1 === 'number'
typeof '1' === 'string'
typeof {name: 'Flavio'} === 'object'
typeof [1, 2, 3] === 'object'
typeof true === 'boolean'
typeof undefined === 'undefined'
typeof (() => {}) === 'function'

Why typeof returned “function”? JavaScript has no function type. That’s true, and that’s a quirk of typeof which conveniently returns that value.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code

Here is how can I help you: