Learn javaScript : Variables (Part 2)
Today's article will focus on variables in JavaScript, where we would look at the various ways or options available in creating a variable with this amazing programming language.
Variable in computer programming is a container that holds data, this data can mostly be of any type which helps in developing the logic for a particular function. Variables in JavaScript can hold integers, strings, functions, classes, and boolean values. Do not worry if you do not understand these things, we will focus on them later on in this series.
Ways of declaring a variable in JavaScript
There are four (4) ways of declaring a variable;
1. Using the var keyword to declare a variable.
sample code
var x = 2; // output 2
2. Using the let keyword to declare a variable.
sample code
let y = 3; // output 3
3. Using the const keyword to declare a variable.
sample code
const x = 2; // output 2
4. Just writig the name of the variable, tho this approah is not very much recommended.
sample code
x = 2; // output 2
Notes :
If you want your JavaScript variables to run on all browsers, it is advised to use the var keyword. This supports both old and new browsers from 1995 to 2015. Let and const keywords are supported from 2015 to current.
Overwriting var and let keywords
But var and let keywords allow for overwriting of data, let's take a look;
sample code
let y = 3; // output 3
let y = 2; // output 2
Strict nature of the const
sample code
const y = 3; // output 3
y = 2; // error message = Uncaught TypeError: Assignment to constant variable.
You would also get a similar error when you try to declare an already declared const variable
const x = 4; // output 4
const x = 5; // error message = Uncaught SyntaxError: Identifier 'x' has already been declared
As the name suggests constant, that is how it behaves in the programming environment. Use it when you want to ensure a general rule and prevention of mistakenly overwriting certain important variables.
The names you supply for variables, types, functions, and labels in your program. Identifier names must differ in spelling and case from any keywords. You can't use keywords (either var or let) as identifiers; they're reserved for special use.
Sample code
const var = 3; console.log(var);
// Uncaught SyntaxError: Unexpected token 'var'
Note
All JavaScript variables must be given a different identifier names. This helps to avoid confliction of variable names and also prevent unecessary overwriting of data which can cause onfusion to the developer.
JavaScript indentifiers can be just single names or descriptive.
Sample code
var t, y, u, o,p; // identifiers in single names
var date, name, my_first_name; // indentifiers in descriptive form
The general rules for constructing names for variables (unique identifiers) are:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter.
- Names are case sensitive (x and X are different variables).
- Reserved words (like JavaScript keywords) cannot be used as names.
- Names can also begin with $ and _ .
Comments
Post a Comment