Background
Two weeks ago or so, I ran into a need for a sample code.
I googled based on the scenario, and interestingly enough the most prevalent sample code was one written in nodeJS.
I do not know nodeJS and could not understand the code.
I thus googled for language-specific sample code.
I found one written in C.
My C is both sloppy and sluggish, yet I managed through.
NodeJS
It is time to learn NodeJS.
Variable Declaration
Traditional
As I looked through sample codes online, I was surprised that folks were declaring regular variables using the const keyword.
I was taken back.
In traditional languages, we do not use the const keyword liberally.
Options
Found out that there are a few ways for declaring variables in NodeJS.
In the blog post I read, one can declare variables using const, let, and var keywords.
Keyword | Sample | Usage | Constraint |
---|---|---|---|
const | const machineName = os.hostname(); | The const keyword can only be used once. The variable is declared and the value is set within the same statement. |
The same variable can not be redeclared in later statements. Also, the same variable cannot be set to a value in later statements. |
let | Let machineName = os.hostname(); | The let keyword can only be used once. The variable is declared and its value set within the same statement. In subsequent statements, the variable’s value can be set to a new value. |
The same variable can not be redeclared in later statements. The same variable can be set to a new value in later statements. |
var | Var machineName = os.hostname(); | The variable keyword can be used multiple times. Using the var keyword, the variable can be redeclared and its value set in subsequent statements. |
None applicable. |
Sample Code
const
Success
// Allocating os module const os = require('os'); //declare and set variable machineName //getting machine name from os class const machineName = os.hostname(); // Printing Machine Name console.log("My machine name is " + machineName);
Failure – Identifier has already been declared
Outline
- Declare variable machineName using const
- const machineName = “”;
- Attempt to use const again
- const machineName = os.hostbame();
Code
// Allocating os module const os = require('os'); //declare variable machineName const machineName = ""; //declare and set variable machineName //getting machine name from os class const machineName = os.hostname(); // Printing Machine Name console.log("My machine name is " + machineName);
Output – Image
Output – Text
/home/runner/2wwffcfi1n/index.js:7 const machineName = os.hostname(); ^ SyntaxError: Identifier 'machineName' has already been declared
Explanation
- If we attempt to use const on the same variable that we have already applied it on, we will get an error message
- The error message will read
- SyntaxError:- Identifier has already been declared
Failure – Assignment to constant variable
Outline
- Declare variable machineName using const
- const machineName = “”;
- Attempt to set variable’s value
- machineName = os.hostbame();
Code
// Allocating os module const os = require('os'); //declare and set variable machineName //getting machine name from os class const machineName = os.hostname(); /set variable's value machineName = os.hostname(); // Printing Machine Name console.log("My machine name is " + machineName);
Output – Image
Output – Text
TypeError: Assignment to constant variable. at Object.<anonymous> (/home/runner/2wwffcfi1n/index.js:8:13) at Module._compile (internal/modules/cjs/loader.js:999:30)
Explanation
- If we attempt to set the value of a variable we have previously declared using const, we will get an error message
- The error message will read
- TypeError:- Assignment to constant variable
let
Success
// Allocating os module const os = require('os'); //declare variables let machineName = ""; //get machine name machineName = os.hostname(); // Printing Machine Name console.log("My machine name is " + machineName);
Failure – Identifier has already been declared
Outline
- Declare variable machineName using let
- let machineName = “”;
- Attempt to use const again
- let machineName = os.hostbame();
Code
// Allocating os module const os = require('os'); //declare variable machineName let machineName = ""; //declare and set variable machineName //getting machine name from os class let machineName = os.hostname(); // Printing Machine Name console.log("My machine name is " + machineName);
Output – Image
Output – Text
/home/runner/sd5la3h45jp/index.js:8 let machineName = os.hostname(); ^ SyntaxError: Identifier 'machineName' has already been declared
Explanation
- If we attempt to use let on the same variable that we have already applied it on, we will get an error message
- The error message will read
- SyntaxError:- Identifier has already been declared
var
Success
// Allocating os module const os = require('os'); //declare variables var machineName = ""; //get machine name machineName = os.hostname(); // Printing Machine Name console.log("My machine name is " + machineName);
SourceCode Control
GitLab
Get Machine Name
- Variable Declaration
- const
getMachineName.variable.declaration.using.const.js - let
getMachineName.variable.declaration.using.let.js - var
getMachineName.variable.declaration.using.var.js
- const
Summary
Hopefully by understanding the various options for declaring variables, one can make better choices by following conventional programming standards and styles.