3 min read
var myVariable = "Hello World"; console.log(myVariable); // "Hello World"
The keyword "let" creates a variable that can only be accessed within the block of code it was declared in.
let myVariable = "Hello World"; console.log(myVariable); // "Hello World"
The keyword "const" creates a variable that cannot be reassigned.
const myVariable = "Hello World"; myVariable = "Goodbye"; // Error, myVariable is read-only
let myVariable = "Hello World"; //initialize myVariable with the value "Hello World" const myNumber = 5; //initialize myNumber with the value 5
It's also possible to declare a variable without initializing it, but in this case, the variable will have a default value of undefined.
let myVariable; console.log(myVariable); // undefined
let myVariableName; // correct let MyVariableName; // incorrect let my_variable_name; // incorrect
let myVariable = "Hello World"; console.log(typeof myVariable); // string let myNumber = 5; console.log(typeof myNumber); // number
Always declare variables using "let" or "const" instead of "var".
Initialize variables when declaring them.
Use descriptive and camelCase variable names.
Avoid starting variable names with numbers.