Variables in JavaScript - Informatic Point

Variables in JavaScript

Variables in JavaScript. Before beginning inside the JavaScript variable we should understand what the word “var” means. In common, variable means something which is held on changing. In programming, the variable is nothing but connecting to the memory in which we stored some value through variables. We have different ways to declare the variables. Each one has its value to use. These are given below.

  1. var
  2. let
  3. const

The above are three keywords in JavaScript. Overall, we can declare a variable with the keyword var. Later in ES2015 (EcmaScript 2015 ) has introduced two new keywords for declaring variables i.e. let and const. These both are block-scoped. But for instantly, we are going to meditate on var.

Read More: Literals in JavaScript.

How Do Variables work in JavaScript?

JavaScript variables are scopes based. JavaScript works on its lexical setting. Some people find it complicated, but it is straight forward. So, now let’s take a look at the variable syntax and examples.

Syntax for Variables in JavaScript

JavaScript does the var, let, and const keywords to declare variables. An equal (=) sign is used to assign values to variables. Let’s dive into syntax.

var variable_name;
let variable_name;
const variable_name;

var Variable Example:

<p id="var"></p>
<script>
var a = 454;
var b = 332;
var c = a + b;
document.getElementById("var").innerHTML =
"The value of C is: " + c;
</script>

let Variable Example:

<p id="let"></p>
<script>
let a = 454;
let b = 332;
let c = a + b;
document.getElementById("let").innerHTML =
"The value of C is: " + c;
</script>

const Variable Example:

<p id="const"></p>
<script>
const a = 454;
const b = 332;
const c = a + b;
document.getElementById("const").innerHTML =
"The value of C is: " + c;
</script>

In the above three examples we have used var, let, and const keywords for creating a variable. The variables named are a, b, and c.

JavaScript is a powerful language and dynamic in quality. So learning how it works is very essential. Scoping of the variable is a very important aspect while learning JavaScript.

Have a nice day.

Leave a Reply

Your email address will not be published. Required fields are marked *