JavaScript Variables And Four Ways To Declare Them.

Table of contents

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;

  1. Local variable: these types of variables are declared inside a block or function and can only be accessible from the block or function.

  2. 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 using let cannot be redeclared in the same block. hoisting using the let variable would just bring up a ReferenceError 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 variable let, 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 or const. 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:

  1. Any variable declared within a function is a local viable

  2. Any variable declared outside a function is a global variable

  3. var within a function is still global

  4. var has a lot of errors that comes with it

  5. The use of var has been replaced with let and const

  6. You can't redeclare a variable that is declared using let and const

  7. 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.

15
Subscribe to my newsletter

Read articles from Ifeanyi Princewill E. directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ifeanyi Princewill E.
Ifeanyi Princewill E.

I have a distinctive combination of technical aptitude and writing proficiency. My aim is to curate both technical and non-technical content and present them in a way that is plain and concise to the layperson. I am also interested in Public goods and I'm a member of the writers' guild at @Greenpill.network