Table of contents
No headings in the article.
If you are new to JavaScript as a Programing language, you would have heard of variables and their various uses. In simple terms, "Variables" are storages that data is saved in for future purposes. These variables are capable of holding the variety of datatypes present in JavaScript. In this article, we would get down to the very basics of what these variables are, and the four different ways we can Declare Variables.
Generally, Javascript Variables are the building block for a whole lot of functions, as these enable us to avoid writing a function more than once. To understand Variables, it is best to know that there are rules that help guide the declaration of variables and these are:
Variable names must Start with a letter( a to z or A to Z), An underscore ( _ ) or a dollar sign ($).
After the first rule, anything can follow be it a number, symbol or letters.
Variable names are case-sensitive this means that variables that start with 'r' is different from those that start with 'R'.
The Javascript "Keywords" cannot be used as variable names.
Javascript uses camelCase when declaring variables e.g billyJean, peeJay, e.t.c, this is only trumped when the variable is one worded then lowercase is used, or when the variable 'const' is used then the variable is in UPPERCASE only.
Variables are of two distinct types, they include;
Local variable: these types of variables are declared inside a block or function and can only be accessible from the block or function.
Global Variable: These types of variables are accessible from any function.
To create variables in Javascript, is referred to as "Declaring". there are Four ways to declare variables in Javascript and these include:
The first is
var
and this used when working on legacy code, assigning a variable that would be used throughout the code, or when you need a variable that can be reasigned to another value later on in your code. and this variable can perform an action called "hoisting", this is where you declare a variable after you have called it.let
, this variable is a local variable, and must be declared before it is used, this variable is used when you need to use a particular variable once in a block, this is because any variable declared usinglet
cannot be redeclared in the same block. hoisting using the let variable would just bring up aReferenceError
message.The
const
variable is a non-assignable variable that must be given a value when it is declared. this variable is used when you know you dont want the value of the variable to be changed, for example an Array, Object or Function.const
doesnt mean the contents cant be changed, it just makes sure that the variable still has its assigned value. like the variablelet
, const cannnot be redeclared in te same block and cannot be used for hoisting as this would bring up an error message.The fourth variable might be a little confusing or suprising, as you might not be quite conversant with it. the fourth way of declaring variable is "nothing", I know it might sound strange, but variables can be declared without using
var
,let
orconst
. In this method of declaration, the variable is global as it is declared outside a function. this variable method could be redeclared, but doing this isnt advised.
I hope this was able to help you with your questions on Javascript variables, for a quick summary of what you should've gotten from this blog and some other things that are important to note:
Any variable declared within a function is a local viable
Any variable declared outside a function is a global variable
var
within a function is still globalvar
has a lot of errors that comes with itThe use of
var
has been replaced withlet
andconst
You can't redeclare a variable that is declared using
let
andconst
You can create variables without declaring them ( this is the Nothing method)
Below is a pictorial representation of the differences between var
, let
and const
.