Hello World Master

Tutorials, articles and quizzes on Software Development

Javascript > Articles

Variables in Javascript

This article is associated with the following course
Introduction to Javascript

We can declare variables as a shorthand to store information. The reasons we want to do this is to make our code more readable and potentially more reusable.

If we wanted to print out a long string of text without variables would look like this

console.log("This is a very long sentence with lots of words in it");

This is what it would look like with a variable

var longString = "This is a very long sentence with lots of words in it";

console.log(longString);

In addition, you’ll eventually run into a situation where you will have to use the same values in many places in your code.

Lets say we wanted to call console.log not once but twice. Without variables this is how we’d do it

console.log("This is a very long sentence with lots of words in it");

console.log("This is a very long sentence with lots of words in it");

With variables we don’t need to spend all that time writing the same sentence all over again

var longString = "This is a very long sentence with lots of words in it";

console.log(longString);

console.log(longString);

This might not seem like much now, but as you develop complex software and the amount of lines in your code increase, it would be difficult to pass values directly to a function without using variables.