Variables

Variables are one of the handiest aspects of computer programming. Variables allow us to save a value and use it over and over again in our code.

Follow these steps when you want to declare a variable:

  1. Start with the var keyword
  2. Give the variable a name (be descriptive, and use camelCase!)
  3. Use the = sign
  4. Give the variable a value
var firstName = "Maggie";

console.log(firstName);
>> "Maggie"

Why Use Variables?

Let's pretend that you're writing code that greets some friends.

Then, you come back and want to greet your friends again, only it's no longer morning. Hmm. With this example you'd have to edit three different lines of code. If instead we were to store the time of day in a variable, we could reuse it in our code.

This would produce the same output as the code in the first example above, but it allows us to change the value of our timeOfDay variable to be "afternoon"or "evening" in a single place.

results matching ""

    No results matching ""