We're making some improvements. Some features may be temporarily unavailable.
beginner20 min

Variables & Functions

Storing data, declaring functions, and understanding scope

Variables in JavaScript

Variables let you store and work with data. JavaScript has three ways to declare them:

let — Mutable Variables

Use let when the value needs to change:

Output
Run your code to see output here

const — Constants

Use const for values that should never change:

Output
Run your code to see output here

var — The Old Way (Avoid)

Before 2015, var was the only option. It has confusing scoping rules — use let and const instead.

Functions

Functions are reusable blocks of code. They let you write logic once and call it many times.

Output
Run your code to see output here

Arrow Functions

A modern, concise way to write functions:

Output
Run your code to see output here

Check Your Understanding

Which keyword should you use for a variable that will never change?

Quick Exercise

Create a function called calculateTip that takes a bill amount and returns 15% of it.

Exercise

Write a function called calculateTip that takes billAmount as a parameter and returns the tip amount (15% of billAmount). Call it with 100 and log the result.

Python will load on first run

Questions & Discussion

Sign in to ask a question or join the discussion.

Loading comments...