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:
const — Constants
Use const for values that should never change:
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.
Arrow Functions
A modern, concise way to write functions:
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.