JavaScript Part-2: JavaScript Variable, Constant and Comment

JavaScript Part-2:  JavaScript Variable, Constant and Comment

What is a variable in JavaScript?

Variable in JavaScript is the name of the container or container for storing temporary data in the computer’s memory, which we use to store any temporary data in the computer’s memory. Once you have data in a variable, you can use it over and over again throughout the code, instead of having to keep the original value over and over again.

    To create a variable in JavaScript, you need to start with any English letter (a-z, A-Z) or underscore (__) with a dollar ($) sign or a dollar ($) sign.

    Example:

Image

The name of the variable in JavaScript is case sensitive. For example, in the case of JavaScript, a ($ a) and A ($ A) are two different variables.

Image

JavaScript cannot contain any spaces called Variable. If the name is more than one, write “___” in underscore (first_name) or uppercase (firstName).

Image

Also in JavaScript you have to use the var keyword in front of the variable to keep a variable limited to just the function (local variable). Otherwise the variable will be considered as a global variable.

Variable global Scope Example

Image

Explanation: Notice that the output of the variable a outside our function does not come out but the output of the variable a inside the function. Because the var keyword is not added to the variable inside the function, it is being considered as a global variable.

Now if we add the var keyword to the variable inside the function, then we can’t access the internal variable from outside if we want.

Image

To put any value in a variable in JavaScript, you need to use the assignment operator (=). E.g. var a = 5;

You can declare more than one variable (leaving comma (,) Seperated) in a statement in JavaScript.

Image

If a variable in JavaScript is re-declared, and a new value is not assigned, then the previous value remains.

Image

The value of a variable in JavaScript can change at any time in the script.

Image

To declare a variable in JavaScript, there is another keyword called let, similar to the var keyword, whose function is to use the variable as block scope { instead of function scope or global scope. This means you can declare multiple variables on the same code page or with blocks in the same function if you want. And let the keyword is included in ECMAScript 2015 i.e. ES6 to JavaScript.

Image

What is constant in JavaScript?

In JavaScript, constant is an Identifier or name for a common fixed value or value, just like a variable. Where the value of the variable can be changed anywhere across the code.

How do I declare Constant in JavaScript?

In JavaScript, you have to declare constant with the const keyword. And the const keyword is included in ECMAScript 2015 i.e. ES6 to JavaScript. A Valid Constant name starts with a letter or (_) Underscore. If you want, you can sign dollar ($) before the name Constant.

In JavaScript, Constant’s name is case sensitive. For example, in JavaScript’s view, a and A are two different constants. There can be no space for the name Constant. If the name is more than one, then write “_” underscore (first_name) or uppercase letter (firstName). Apart from that, any name declared as Constant in JavaScript can no longer be used as a variable. Let us understand from the following example:

Image

However, if you want to declare JavaScript Variable and Constant with the same name, you need to use a code block. That is, Constant T should be placed in the curely braces “{}”.

Image

In other words, the const keyword in JavaScript only maintains block scope, not function scope. That is, if you use a constant in a function within a block, then that constant cannot be accessed outside that block. I am Amidur Rahman from Bangladesh .

Image

Note: If Constant is an Object or Array in JavaScript, you can change the value of its property if you want, even if you can’t re-assign it.

Image
Image

However, if you re-assign an Object or Array, it will display the following error.

Image
Image

What are the comments in JavaScript?

Comments in JavaScript or any programming language are the explanation or footnotes of the source code of a coder or programmer. We can also call it Coding Documentation. Usually the Compiler and Interpreter of any programming language ignores it without executing the comments as code. With the Comments System, we will not be using the Coding Documentation at the moment, but we can temporarily hide the codes that may be useful in the future.

How many types of Comments in JavaScript?

Comments in JavaScript are basically of two types:

    1.Single Line Comments (Use a different comment mark for each line)

    2.Multi Line Comments (You only need to use one comment mark for multiple lines at once.)

What is Single Line Comments in JavaScript? How to make Single Line Comment in JavaScript?

In JavaScript, a programmer uses the Comment symbol separately on each line. In JavaScript terminology it is then called Single Line Comments. This is called short comments again. Single Line Comments are done in JavaScript with // (two slashes). Let’s understand a little better with the following example:

Image

Explanation: Notice that the two lines that we have bound here in single line comment, those two lines will not come in our browser.

What is Multi Line Comments in JavaScript? How to make Multi Line Comment in JavaScript?

In JavaScript when a programmer uses only one comment mark for multiple lines. In JavaScript terminology it is then called Multi Line Comments. This is called long comments again. Multi Line Comments are done in JavaScript with / * * /. Let’s understand a little better with the following example:

Image

Explanation: Notice that all the lines we have bound here in the multi line comment will not come in our browser.

Leave a Reply

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