IN THIS LESSON

What is a Variable?

In programming, we need ways to store, and then retrieve, information. One way we can store that information is by creating something we call a variable.

In JavaScript, in order to declare, or "create", a variable you have to use a special keyword. The two keyword options you can use are:

  • let or

  • const.

Understanding when to use let and when to use const is an important part of writing reliable and maintainable JavaScript code. Let’s get into some examples of when to use each so that you can better understand!

Let’s start with Const:

Const is a keyword you use in JavaScript to declare a CONSTANT variable (which is a value that should remain constant throughout the program you code). Once a value is assigned to a constant, it cannot be changed meaning once a const variable is used it can never be reassigned. So for example, your specific birthday is a constant. It’s always the same and can never be changed.

Let’s show you an example of how you can code a constant (const) variable that contains your birthday:

const birthday = "Feb 14th" 
 birthday = "Sep 25th"
console.log(birthday)// THIS WILL THROW AN ERROR BECAUSE CONST VARIABLES CAN NOT BE REASSIGNED

Let Keyword:

The let keyword in programming is used to declare variables. Variables declared with let can be reassigned, meaning you can change their values after they have been initially set.

For example, as people, we have many different moods and feelings and it is constantly changing.

let myMood = "Peaceful"
myMood = "Happy"
console.log(myMood)// Happy

Notice that when we changed the variable's value, we didn't use another let keyword. We only use the let keyword once because the ‘let’  keyword allows a variable to be reassigned.

No using before declaring!

Regardless of let or const, don't forget that you can't reference a variable before you've actually created it!

myNumber = 100
let myNumber = 101
NO GOOD, YOU USED myNumber BEFORE YOU DEFINED IT

Variable Name Rules

Here are some starter rules for naming your variables

  • Names can only include letters, numbers, and underscores

  • You can't start names with numbers

  • No spaces allowed

  • If a name needs multiple words, use camelCase, which means each new word is capitalized (except the first

let thisIsHowToCamelCase =let no spaces =let 1badExample =let goodNumberExample2 =let technicallyallowedbutconfusing = 😒

Let's practice creating variables using "let" and "const". Test what happens if you attempt to reassign these variables. Don't forget to click "Run Pen" in CodePen to execute the code and observe the output.

What can you assign to a variable?

Data Types

So far we've only used numbers or text for our variables, but those aren't the only things we can store. There are many different kinds of data types in JavaScript, but here are some of the simple ones to start us off.

Numbers

JavaScript only has one main Number type. Both whole numbers (called "Integers") and decimals (called "Floats") are just Numbers.

12
14242.23

You can also do math with JavaScript. Here are some of the basic operations:

Strings

In JavaScript, a string is a sequence of characters enclosed within single (' '), double (" "), or backtick (``) quotes. Strings are used to represent text data and can include letters, numbers, symbols, or a combination of these.

+ = addition
- = subtraction
/ = division
* = multiplication
let borough = "I live in Brooklyn and I love it here"
let myFavoriteSubject = 'My favorite subject is History'

Booleans

A boolean in JavaScript is a primitive data type that represents one of two values: true or false. Booleans are often used in programming for making decisions or evaluating conditions. In JavaScript, bools are lowercase. Don't put them in quotes, they aren't text, they're actual values.

Bools are for something called Flow Control, but more on that in Lesson 3!

let myBoolean = true
myBoolean = false

Undefined and Null

Sometimes the value we want to save is ...no value. That's what undefined and null are. There is a difference between them, but it's irrelevant for now.

let nothing = undefined
let alsoNothing = null

Undefined errors

Sometimes when JavaScript expects something but gets nothing, the value will show up as undefined. This is your hint that something is going wrong in your program and you should investigate further.

🎉 Congratulations! You've successfully grasped the basic fundamentals of Variables. Now, let's reinforce your learnings with some hands-on practice. Below are prompts to help you strengthen your skills. Feel free to use the CodePen link provided to write and test your code, press Run Pen to activate the terminal:

Challenge 1: Declare a variable named myNumber and assign it any numerical value.

Challenge 2: Create a string variable called myString with a sentence of your choice.

Challenge 3: Perform a mathematical operation using myNumber (add, subtract, multiply, or divide) and log the result to the console.