DEV Community

Cover image for JavaScript Made Easy: Part 2
David Tetreau
David Tetreau

Posted on • Updated on

JavaScript Made Easy: Part 2


Variables are used to store data values. These values can later be accessed by just using the variable name they were stored in. An example of a variable is as follows:

var age = 38;
console.log(age);//logs 38
Enter fullscreen mode Exit fullscreen mode

In the code above, var is the way to declare a variable, age is the name of the variable, and the variable is given the value of 38 by using the equals sign. This equals sign is known as an assignment operator. The value 38 is a number, which is a data type that is different than the string introduced in Part 1. Numbers are not put inside quotation marks. Data types will be discussed further in another section.

Declaring Variables

As of 2015 when the JavaScript(ES6) version was implemented, there were three ways to declare a variable:

  1. var is the older way to declare a variable, but using var is still valid. Some older code that you may work with in the future may use this convention, so it is important to know. Values assigned to variables declared with var can be reassigned or changed. When possible, it is best to use the next two variable types, const and let.
  2. const is used to declare a variable that will not be changed. An example is as follows:
const firstName = "David";
Enter fullscreen mode Exit fullscreen mode

Since my first name will never change, const would be appropriate to use. You may also notice that I named the variable so that it is self-describing. This is a good coding convention because it helps other Developers easily identify what the variable is being used for.

  1. let is used to declare a variable that can be changed. An example is as follows:
let age = 38;
Enter fullscreen mode Exit fullscreen mode

Since age is something that will definitely change, using let would be appropriate. Remember that let is the variable declaration, age is the variable name that you will use to refer to the variable later in the code, the equals sign is the assignment operator, and the number 38 is the value. Also, this statement is closed by a semi-colon.

Practical Exercise

You can now use replit to practice what you have learned! Here's how:

  1. Open a new JavaScript repl (as demonstrated in Part 1)
  2. In the white area type:
const name = "Put your name here";
Enter fullscreen mode Exit fullscreen mode
  1. Below that type:
let age = a number goes here;
Enter fullscreen mode Exit fullscreen mode

Since your name will not change we used const, and since your age will change we used let.

  1. Below that, type console.log(name);
  2. Below that, type console.log(age);
  3. Click run at the top.

variables 2

As you can see above, we used just the variable names without quotation marks to log the values saved in the variables to the console. We could have also used var for both of these variables, but using let and const is a better practice.

Also, you could have combined the variables into one console.log(). This wouldn't have made much sense to display them that way, but it is good to be familiar with the fact that you can log multiple variables to the console in one line by separating them with a comma. This is demonstrated below:


Reassigning Values

As stated previously, you can reassign values to variables you have declared with let or var. This is demonstrated below:


Please take some time to play around in replit and practice assigning variables and logging them to the console. Make sure you are using good coding conventions. Take note of the white space placed in each statement. Also, use blank lines to separate one block of logically related code from another. Further reading on styling conventions can be found here.

I hope you have enjoyed this post! Please check out the entire "JavaScript Made Easy" series by David Tetreau. There will be a new post daily.

Discussion (0)