Step 4: Variables

When you code, you'll want to create shortcuts so you can easily refer to things without writing out the same value every time. In JavaScript we have "variables", which allow us to store values to use again later.

Try it out

You create a variable using the keyword var, which lets the application know that you're about to store a value. You also have to give a name to your variable, which can be anything you like, so long as it doesn't include spaces.

Here's an example of creating a variable called "artist", which is storing a string called "Beyoncé Knowles".

var artist = "Beyoncé Knowles";​
console.log(artist);

Notice that once you've created the variable, you can then simply write the name of the variable wherever you want to refer to it, rather than writing out Beyoncé's full name every time. There are no quotes around a variable name, because we don't want to get it confused with a string.

Run the above code. "Beyoncé Knowles" should print to the console.

Last updated