const
A variable declared as a const implies two things. First that a variable can not be re-declared and second that it's value is a one time assignment, that is its value cannot be changed. To demonstrate this, let's take a look at a sample code.
Listing 1
const pi = 3.14;
var pi = 3.14;
// throws SyntaxError, Identifier 'pi' has already been declared
If you run the code in listing 1, Nodejs will throw a SyntaxError with the error Identifier 'pi' has already been declared. This clears up the fact that a variable declared as const cannot be re-declared. If you tried to assign a new value to pi as shown in listing 2, Nodejs will throw a TypeError with the error Assignment to constant variable..
Listing 2
const pi = 3.14;
pi = 3.14;
// throws TypeError, Assignment to constant variable.
The definition of the word constant means "something that does not change" and so in JavaScript, the value of a variable declared as a const cannot change. Or can it? Well it turns out that this is primarily true for primitive types such as strings, integers etc. But not so true when it come to complex types such as objects and arrays. Take a look at the following example.
Listing 3
const user = { first_name : "John", last_name : "Smith" };
user.first_name = "Will";
console.log(user.first_name, user.last_name);
Listing 3 above shows a const variable that has been assigned an object. The object can be modified by assigning new values or even adding new properties. The same applies to arrays as shown in listing 4.
Listing 4
const users = [];
users.push("John");
users.push("Suzan");
console.log(users.toString());
Although you can modify the data structure, you still can't assign a new value to a const variable.
let
Variables that are declared as let are block scope local variables and unlike const variables, they can be re-declared. Listing 5 below shows the x variable declared as let and assigned the value 10. Notice that it is then re-declared within the if statement block and assigned a new value. This value will only be available within the ifstatement block. This is whats called block scope. The output of the code will produce 10 and 100.
Listing 5
let x = 100;
if(true){
let x = 10;
console.log(x);
// outputs 10
}
console.log(x);
// outputs 100
We can demonstrate this further by simply removing the first initialization of the x variable as shown in listing 6.
Listing 6
if(true){
let x = 10;
console.log(x);
}
console.log(x);
What do you expect the output of the code above to be? If you said 10 followed by a ReferenceError, you would be correct because x is only available within the if statement block.
var
Probably the most used keyword in JavaScript (Don't quote me on that), var is what was used before const and let were introduced. Like the let keyword, var variables can be re-declared and re-assigned a value. The difference between var and let is that var variables are executed within the context they were defined. Either a function scope or a global scope. To understand this better, let's take a look at an example. Listing 7 below is similar to listing 6 above with the notable difference being that the variable x is declared as var.
Listing 7
if(true){
var x = 10;
}
console.log(x);
// outputs 10
The code above will output 10 because the x variable is defined globally.
-
A JavaScript Implementation Of The Logo Programming Language - Part 2
Part 2 of A Javascript Implementation Of The Logo Programming Language. In the previous article I explained how to develop a simple lexer. In this part we develop a TokenCollection class to help traverse the array of tokens returned from the lexer.
10 June 2020 - 2802 views -
A JavaScript Implementation Of The Logo Programming Language - Part 1
In this four part article, I explain how to develop the iconic Logo programming language in JavaScript. In this part, we discuss how to take a source input and convert it into a series of tokens.
06 June 2020 - 5063 views -
Generating Web API Keys
If you're building a REST API, chances are you're going to need to generate secure random API keys. In this article, I explain how to use the Node.js crypto module to generate random secure API keys.
29 May 2020 - 7733 views -
Port Scanner
A simple port scanner to scan a range of ports.
31 January 2020 - 3563 views -
Sorting Algorithms
An introduction to sorting algorithms.
29 November 2019 - 2451 views -
Simple Http Server
This article explains how to create a simple HTTP server using Node.js without using any dependencies.
04 September 2019 - 2287 views -
Reading From Console
Node.Js provides several ways to read user input from the terminal. This article explains how to use the process object to read user input.
16 July 2019 - 2873 views