Variable
The most accessible way to use data in a script is to first assign it to a variable. It’s generally easier to suppose a variable as a handbasket of information. How long a variable retains information depends on several factors. Still, as soon as the web runner clears the window, all known variables are incontinently discarded.
You can produce a variable in JavaScript by using the var keyword followed by the name you want to give to that variable. Thus, to declare a new variable similar as myAge, the JavaScript statement is
That statement lets the cybersurfer know that you can use that variable later to hold data or to alter any of the data in that variable.
To assign a value to a variable, use one of the assignment drivers. The most common done by far is the equal sign.However, I use that assignment driver in the same statement as the var keyword similar as If I want to assign a value to the myAge variable at the same time I declare it as:
On the other hand, if I declare a variable in one statement and indeed want to assign a value to it, the sequence of statements will be:
JavaScript Variable Names While naming your variables in JavaScript, follow the following Rules:
You shouldn’t use any of the JavaScript reserved keywords as a variable name. For illustration, break or boolean variable names aren’t valid.
JavaScript variable names can not start with a number( 0- 9). Must start with a letter. For illustration, 123test is an invalid variable name, but, 123test is valid.
JavaScript variable names are case sensitive. For illustration, name and name are two different variables.
Constant
In JavaScript, a constant is a variable whose value can not be changed after it’s assigned to it. You can define constants using the const keyword, illustration:
After defining a constant, its value can not be reassigned. trying to do so will raise a TypeError,illustration:
pi = 3.15;// TypeError Assignment to const variable.
It’s recommended to use constants whenever there are values that shouldn’t be changed.
Constants make your law easier to read and help crimes that can do when variable values change suddenly. still, the constant value can not be changed, but it’s parcels and rudiments can still be changed, If the value is an object or array,illustration: