Back

Activity 1.3: Variables and Console Output

divider

Activity 1.3

Variables and Console Output

Key Concepts

Variables

Console Output

What Is a Variable?

A variable is a named container that holds a value, so you can refer back to it by name instead of retyping the value every time.

Declaring a Variable

let name = "Ada";

let declares the variable,name is what we're calling it, and everything after the = is the value we're assigning to it.

Naming Variables

JavaScript variable names usecamelCase — the first word lowercase, each word after that capitalized, no spaces or underscores.

favoriteColor, not favorite_color or FavoriteColor.

Console Output

console.log() prints values to the terminal. It can take more than one thing at a time, separated by commas.

console.log("Age:", age);

Today's Objectives

  • Declaring variables with let
  • Naming variables using camelCase
  • Printing variable values with console.log()

Key Terms

Variable
A named container that holds a value.
Declaration
Creating a new variable, using let.
Assignment
Giving a variable a value, using =.
camelCase
JavaScript's naming convention — lowercase first word, capitalized words after.
console.log()
Prints one or more values to the terminal.

'F' → Fullscreen

divider

Build

Create a new JavaScript file named 1-3-variables.js.


Task 1: Declare Your First Variable

  • Declare a variable named name and assign it your name, as a string.
  • Print it with console.log(), including a label.
1-3-variables.js
let name = "Ada";
console.log("Name:", name);

Task 2: Add a Second Variable

  • Declare a variable named age and assign it your age, as a number.
  • Print it with a label, just like name.
1-3-variables.js
let name = "Ada";
let age = 16;
console.log("Name:", name);
console.log("Age:", age);

Task 3: Add Two More Variables

  • Declare favoriteColor and favoriteNumber, using camelCase.
  • Print each one with a label.
1-3-variables.js
let name = "Ada";
let age = 16;
let favoriteColor = "blue";
let favoriteNumber = 7;
console.log("Name:", name);
console.log("Age:", age);
console.log("Favorite Color:", favoriteColor);
console.log("Favorite Number:", favoriteNumber);
divider

Checkpoint

Verify your program works correctly.

Example Output
Name: Ada
Age: 16
Favorite Color: blue
Favorite Number: 7
divider

Reflection

Answer the following questions before submitting your work.

  1. What is a variable, and why do we give it a name instead of just typing the value directly every time?
  2. What naming convention did we use for names likefavoriteColor? What's it called, and why do you think a consistent convention matters when writing code?
  3. What do you think would happen if you tried toconsole.log() a variable before declaring it?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete