DEV Community

Cover image for Basic JavaScript: Variables
Shubham Verma
Shubham Verma

Posted on • Updated on • Originally published at blogs.shubhamverma.me

Basic JavaScript: Variables

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

If you have any reviews about this series or my way of explaination, then please let me know 🙂.

Top comments (0)