No history yet

Variables and Basic Values

The Language of the Web

JavaScript is the programming language that brings websites to life. If HTML is the skeleton of a webpage and CSS is the skin and clothes, then JavaScript is the nervous system. It handles the logic, interactivity, and dynamic changes that make a site feel responsive and alive, from a simple image slider to a complex web application.

Lesson image

At its core, all programming is about storing and manipulating information. To do this, we need a way to hold onto data so we can refer to it later. In JavaScript, we use variables.

A variable is like a labeled box where you can store a piece of information.

Creating Variables

To create a variable, you need to declare it. This tells the computer you're creating a new storage box. In modern JavaScript, we have two main ways to do this: let and const.

Use let when you expect the value in the box to change over time. For example, a user's score in a game will change as they play.

// Use 'let' for a value that might change
let currentScore = 0;

// Later in the program...
currentScore = 10;

Use const for a value that should never change. Think of it as sealing the box shut after you put something inside. This is useful for things like a birth date or the number of days in a week.

// Use 'const' for a constant value
const daysInWeek = 7;

// Trying to change it will cause an error!
// daysInWeek = 8;  <- This would break the code.

After the keyword let or const, you give your variable a name. This name must be unique and should describe the data it holds. Good naming is a critical skill in programming.

Notice the naming style, like daysInWeek. This is called camelCase and it's the standard convention in JavaScript. You start with a lowercase letter, then capitalize the first letter of each new word, with no spaces.

Basic Data Types

Variables can hold different kinds of data. These are called data types. For now, we'll focus on the three most common and fundamental types.

String

noun

Any sequence of text, like a word, a sentence, or a paragraph. Strings are always wrapped in quotes (either single ' or double ").

Number

noun

Used for mathematical values. This can be an integer (like 10) or a decimal (like 3.14). Numbers are not wrapped in quotes.

Boolean

noun

A simple true or false value. Think of it as a light switch that can only be on or off. Booleans are fundamental for making decisions in code.

The Boolean data type is the simplest of all. It can only ever hold one of two values: true or false. This binary choice is the foundation of all computer logic.

Seeing Your Work

Writing code is great, but how do you see the results? We can ask the computer to print a value to a special area, usually hidden in your web browser, called the console. The command for this is console.log().

You place the variable or value you want to see inside the parentheses.

let playerName = "Alex";
const score = 100;
let isWinner = true;

console.log(playerName); // Prints: Alex
console.log(score);      // Prints: 100
console.log(isWinner);   // Prints: true

This simple pattern of creating data, processing it (even just by storing it), and then outputting it is the core of all programming. You'll use console.log() constantly to check your work and understand what your code is doing.

Time to check your understanding of these fundamental building blocks.

Quiz Questions 1/5

In the analogy of a website being like a human body, what part does JavaScript represent?

Quiz Questions 2/5

You are building an e-commerce site and need to store the value of the sales tax rate, which is fixed by law. Which keyword is most appropriate for declaring this variable?

Great work. You've just taken the first and most important step in learning to program: understanding how to store and view information.