Some people find differences easily. Their eye can see the one thing out of place. Others look for commonalities.
I find myself in the latter camp. Especially, as I look at programming languages. JavaScript is quite similar to other languages. Let’s focus on values and variables.
Values
The data we keep track of is our value. That could be some words or numbers. Perhaps even some special characters for the computer to process.
Variables
We give the data a name or a handle if you will. This allows us to reference it elsewhere.
let tomsJavaScriptVariable = "stuff";
This code example has a value, “stuff”, and a variable name, tomsJavaScriptVariable. A lot happened here so let me explain it.
Declare
First thing we must declare a variable. This can be done by the following.
let declareVariable; // just declaring
The let keyword followed by the name declares the variable. Note our previous example set the value. This one does not.
Initialize
When we give it a value we are initializing the variable. So we can use our declareVariable here and initialize it too.
declareVariable = 123; // initialize variable
As you look through the examples you see that my first example did all this at once. So you can declare and initialize on separate lines or all at once. This can be personal preference. It can aid in the readability of separating them.
Naming
As you name your variables you have a few rules to learn in JavaScript. The length can be as short as one character. There is no upper limit. The first character is limited to letters, underscore(_), and dollar sign($). Numbers are not allowed for the first character.
let a; // letter
let areallylongnameforavariablethatyouwontwanttoread; // long but legal
let _var; // underscore first
let $sal; // dollar sign first
let 4num; // illegal - won't work!
The other characters in the name can be letters, numbers, and characters. Also, the case can be mixed too.
let aMixtureOfUpperCaseAndLowerCase;
let a123$;
let _jsfgsiu$234432;
JavaScript gives you a lot of options to name your variables. Your fellow developers would appreciate meaningful names. I like to make wisecracks, but your code is not a good place to try this.
Const
If you have a variable you don’t want to change you can use the const keyword to declare it. This is in place of the let keyword.
const pi = 3.14; // not able to change
let exhangeRate = 3.4; // able to change
Just make sure you think this through. Use the const keyword wisely. If you get crazy with it you will have issues.
let vs var
Variables can be declared with the var keyword too. Using the var keyword declares the variable in the whole function. While let is only for the block it is defined.
function varScoping() {
var x = 1;
if (true) {
var x = 2;
console.log(x); // will print 2
}
console.log(x); // will print 2
}
function letScoping() {
let x = 1;
if (true) {
let x = 2;
console.log(x); // will print 2
}
console.log(x); // will print 1
}
This article shares more about these two options. It can be confusing to a newbie. Overall use let unless you need var. The global nature of var can cause some side effects.
These are the foundations of understanding JavaScript. Variables and values give us something to work with as we progress on our journey. We learned how to declare and initialize the variables. Plus we covered naming and different keywords to use.