Part 1 - Learn about Variables in JavaScript Step by Step

Learn about a variable in JavaScript. We are going to learn about it in two installments. This ongoing post is the first part, it helps you to learn about the concept of a Variable. The second part takes you through the rules of creating a variable & other useful information.

Introduction

We code to solve a problem of real-world using computers. To do so we need to write programs. A program is like a conversation being held with computers. So, similar to a conversation the way we refer to a human being with a name, we refer information to be used in a program using a variable.

The variable is a 'thing' that holds what we want a JavaScript 'Interpreter' to remember.

An 'Interpreter' is a person (software in the context of programming) who works as a Translator in JavaScript coding work.

Now, we are aware of the work profile of a Translator. It translates one language to another language.

This is how the interpreter translates our English embedded 'lines-of-codes' to Machine language. Such language consists of 0, 1's.

Well, let's focus back on variables in JavaScript. Have a look at the following sentence or line(s) of code:

let myName = "Nikhil"
let age = 10
let weight = 25.5

Here name myName, age, weight, etc are variables and according to the definition written above it holds information for instance name, age, weight, address, marks, etc of a student.

And let is such a word (technically keyword) that denotes the introduction of a variable to the Interpreter.

In programmers' world a variable is nick-named as Identifier, because it helps to identify the information stored at it.


Why do I write 'myName' without space?

Answer: Yeah, good question. Because creators of JavaScript have made a few rules to form names of our variables.

JavaScript creators have made the rule because the Interpreter is incapable of remembering a name that consists of space characters. If it cannot recall the name correctly then we won't be able to run our application in the browser itself. Nobody would be able to use it and henceforth, our sheer hard work becomes pointless.

As we human beings have naming structure the same way we form name of variables as provided structure.

structure 1: let name

structure 2: let name value_to_remember

structure 3: let name value_to_remember

Fun fact: The structure mentioned above is common in all programming languages with a minor change. That is, we don't use the keyword 'let' in all programming languages. Even in JavaScript, you can create a variable using 'var' keyword. Similarly, a programming language named Java/C#/ python we create a variable using a concept name 'data-type.' A Datatype is a person that tells the nature of a variable to its compiler/ interpreter. For instance, if a variable can hold numbers, a group of letters or words, sometimes number with decimals, etc.


In this post, we have become familiar with the variables of JavaScript. In the next post, we are going to learn about rules to create a valid variable name.

Part2