Basic JavaScript: Variables

Basic JavaScript: Variables


1 min read

Let's start with Variables.

A Variable is a container that can hold a certain value, like in Maths we have variable x=2 or x+y=2 where x and y are variables that can hold a number.

In JS, we define a variable using var keyword.

var a = 2; // here "a" holds the value 2

Here's an another example:

var b = 6;
var c = 4;
console.log(b+c); // Now b = 4 and c = 6, so output will b +c i.e 10

Variables can hold various Data Types, which we'll learn in futher lessons.

For example:

var greet = "Hey There!";  // greet holds a String data type.
var age = 21; // age holds an integer data type.

NOTE: If you declare a variable with var keyword initially and the re-assign a different value to it, the previous value gets overwritten.

var name = "Elon Musk"
console.log(name); // Elon Musk gets logged.

var name = "Tesla"
console.log(name); // name is now Tesla, not Elon Musk

Next, We'll learn about Data Types in JavaScript.

If you have any reviews about this series or my way of explanation, then please let me know ๐Ÿ™‚.