So far in this course, we have learned:
- How to install Node.js and npm, the Node package manager
- How to select a text editor (I use VS Code)
In computer programming, variables are names that store a specified value.
We'll explore each keyword in detail in this tutorial. First, let's talk about some restrictions on variable names.
first with a value of
Nick with the following command:
var first = 'Nick';
The output of this statement will be:
age with a value of
let age = 24;
Once again, we can print the value held within
age to the console with the
console.log statement like this:
Here is the output of this command:
Let's create a variable called
last with a value of
McCullum using the
const last = 'McCullum';
As before, we can print the value of
last to the console with the
console.log statement like this:
Here is the output of this statement:
Variables declared with the
const keywords have different characteristics. We will discuss these attributes in the next section of this lesson.
To start, the
let keywords can be updated. The
const keyword cannot.
Let's consider an example:
//initial variable declaration and assignment var first = "Nick" let last = "McCullum" const age = 24 //update the "first" and "last" variables first = "Warren" last = "Buffett" //log the "first" and "last" variables to the console console.log(first) console.log(last)
Notice that the
let keywords when updating a variable's value.
What do you think happens when we attempt to run this code?
If you guessed that the new values of
last will be printed to the console, then you are correct! Specifically, here's what the output would be:
What happens if you try and update the variable that was created with the
As an example, let's try and update the
age = 89;
This code returns the following error:
Uncaught TypeError: Assignment to constant variable. at <anonymous>:1:5
The second important difference between
var variables are scoped differently than
var variables are function scoped while
const are block scoped.
One last note on the
const variables - specifically regarding their history.
If this is a lot to digest, do not worry! We'll learn more about this later. We will learn much more about variable scope later in this course.
const keywords. As an example, the following code runs successfully:
variable = 'This is my variable!';
You can then print this variable's value with a console.log statement like this:
This is my variable!
What is going on here?
const keywords were not as formalized as they are today, so if you're missing these keywords then the browser will automatically make the variable of type
'use strict';, it disables this functionality. As an example, the following code returns an error:
'use strict'; variable = 'This is my variable!';
Here is the specific error that is returned by this code:
VM247:3 Uncaught ReferenceError: variable is not defined at <anonymous>:3:10
The truth is, there is no hard and fast rule. I do recognize that having some guidelines is helpful nonetheless, so here is what I do:
- When declaring a new variable, I use the
constvariable by default
- There are a few special situations where I may use
var, but these are few and far between
I reiterate that these are not rules, but simply my opinion.
". There are two exceptions to this. Both
camelCase is a naming convention in which you start your first word with a lowercase letter, use no spaces, and uppercase the first letter of every word from the second word onwards.
Here are a few examples of
camelCase variable names to help you understand this better:
While there are other naming conventions (like
camelCase when naming their variables in their applications.
You might be wondering what happens if we create variables but do not assign any value to them. For example, what happens when we attempt to run the following code:
let variable; console.log(variable);
undefined to help handle this. As you'd expect, that's exactly what the output of tha last code block is:
The semantics here are a bit hard to understand, but a variable with value
undefined is different than a variable that has not yet been created.
VM371:1 Uncaught ReferenceError: notAVariable is not defined at <anonymous>:1:13
undefined value, a value of
const nullVariable = null;
null values. This is because the
null keyword can be explicitly used to change the value of a keyword that already exists, like this:
let name = 'Nicholas'; name = null; console.log(name); //Prints null