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.

Kindly study the following Part in case you had skipped it.

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

Use the const keyword to declare your variables when you want to define a general rule.
Supposing you want the value of a variable to remain constant throughout the execution process of the system. Using the const keyword would be the best way to do that.

 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.

 

JavaScript Identifiers

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:

  1. Names can contain letters, digits, underscores, and dollar signs.
  2. Names must begin with a letter.
  3. Names are case sensitive (x and X are different variables).
  4. Reserved words (like JavaScript keywords) cannot be used as names.
  5. Names can also begin with $ and _ .

The Assignment Operator

The equal sign "=" is used to assign any value to a variable.

Sample code

let a = "value"; // variable a now holds value as it data.

// or
const u = 3;
const x = 2;
var results = u + x;

Note

Using "==" means "equals to". This changes the expression into a conditional statement.

Expression sample code

var results = 4 + 9; // This is an expression
let x = 5; // This an expression

Condition sample code

const age = 20;

const response = age == 20; // this is a conditional statement

// response variable contains a value of true boolean value

More about declaring javascript variables

A variable declared with var keyword can be redeclared and still maintain it value. But when a variable is redeclared with let and const keyword the browser throws an error Uncaught SyntaxError: Identifier 'your_declared_variable' has already been declared


Sample code

let number = 7; // Uncaught SyntaxError: Identifier 'number' has already been declared


Conclusion


In summary JavaScriipt variables helps data to be stored in memory for further processing. You can perform some basic functions to try your understanding.

Proceed to Part 3 : JavaScript global and local scope




Comments

followers

Popular posts from this blog

The Beauty Of Using Flutter With Dart For Mobile Development

Building a Fortified Database Connection in PHP

Mastering Error Handling in PHP: A Deep Dive into `@` and `try...catch`