What Are JavaScript variables?

Types

JavaScript has a couple of built-in types, which we are going to quickly go over here, and go more in depth on in the following lessons.

Dynamic typing

JavaScript is a dynamically typed language which means that we don't have to declare the type of variables, parameters or return values before hand, unlike statically typed languages.

JavaScript variables can even hold different typed values as the code executes without any issues. Though it is strongly recommended that we use a variable for a single type only, so that we avoid situations where a variable is of a different type than we expected.

Numbers

JavaScript does not differentiate between integers (whole numbers) or floats (decimal numbers) and are written straight out without any surrounding symbols.

const universe = 42
const pi = 3.14

Text/Strings

Strings are written as text inside double or single quotation marks.

const shipOfMagicQuote = "Look forward, not back. Correct your course and go on. You cannot undo yesterday's journey."

Booleans

Booleans can have two values: true or false.

const trulyTruthy = true
const falsestFalse = false

Arrays

Arrays can hold multiple values (of different types or same type). We define an array using square brackets where we separate elements with commas. Arrays are indexed collections, meaning we can specify an index to get the value in that position. It is important to remember that JavaScript arrays are zero-indexed; first element has index zero.

const countries = ['Norway', 'Sweden', 'Denmark']
const firstCountry = countries[0]

Objects

Objects can hold multiple key/values pairs and are used to model real life objects. We define an object using braces where we separate properties with commas. Objects are keyed collections, meaning we can specify a key to get the accompanying value.

const jadeCity = {
name: 'Jade City',
author: 'Fonda Lee',
isbn: '0316440868',
}
const isbn = jadeCity.isbn