Lesson Introduction

Hello, and welcome to your JavaScript journey! Today's quest is to understand variables — our way of storing data, much like jars for keeping cookies. We'll see how to create these jars with labels (let and const), what to write on these labels (naming variables), how to fill them (assigning values), and then how to display our cookies (printing values). Let's dive in and start our delicious coding adventure!

What is a Variable?

In JavaScript, variables are the tags we create to track and use our data.

  • For changeable data, such as the number of cookies in your jar, which is likely to change, we use let.
    • It’s like having a grocery bag that we can fill and empty often.
  • For unchanging values, const is the go-to choice.
    • Imagine a piggy bank for a specific type of coin; once it’s in there, it’s intended to stay.

Here is an example:

JavaScript
let myAge; // Ready for an age that may change over time.
myAge = 30; // Assigning my current age.
const PI = 3.1415; // Like a coin, pi’s value won't change.

By the end of this lesson, you'll understand how to declare variables for dynamic and static values.

Naming Variables

Variables should be clearly named to indicate the data they contain. Good naming is like a well-labeled spice rack: it allows you to find exactly what you need at a glance. Start variable names with letters, underscores (_), or dollar signs ($), and avoid reserved words like const, let, and similar.

In JavaScript, it's considered to be a good style to name variables in Camel Case: when the variable name consists of multiple words, the first letter in the first word is lowercase, and the first letter in every other word is uppercase. For example: customerName, myAge, temperature, veryLongVariableName.

let totalAmount; // Clearly, it's an amount, likely of money.
let customerName; // A better fit for a shopping app than 'userName'.
let temp; // Not descriptive; what does 'temp' signify - Temperature? Temporary?
let $date; // Variable name can start with a dollar sign.
let _99redBalloons; // Leading underscores are okay, but avoid starting with numbers.
let 123number; // Invalid variable name - the name can't start with a digit.

Choose descriptive names to make your code as intuitive as a thoughtfully organized pantry: every item is clearly labeled and easy to reach.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal