What Are JavaScript Variables For Numbers ?

What Are JavaScript Variables For Numbers ?


JavaScript has variables like many other programming languages. Variables may be thought of as named containers. We may place data into these containers and then refer to the data simply by naming the container. We must declare variables before using them in JavaScript programming. Variables are declared with the var keyword. A string is not the only thing we’ll assign to a variable. We’ll also assign variety.
var weight = 150;

Whenever we write weight in our code having coded the statement above, JavaScript knows we mean 150. We’ll use this variable in math calculations. If we ask JavaScript to feature 25 to weight… weight + 25…JavaScript, remembering that weight refers to 150, will come up with the sum 175. Unlike a string, variety isn’t enclosed in quotes. That’s why JavaScript knows it is a number that it may do the math on and not a text string, sort of a postcode, that it handles as text. But then, since it isn’t enclosed in quotes, how does JavaScript know it isn’t a variable? Well, because variety, or any combination of characters starting with variety, cannot be used as a variable name. If it is a number, JavaScript rejects it as a variable. So it must be variety. If we enclose variety in quotation marks, it is a string. JavaScript can’t do addition thereon. It may do addition only on numbers not enclosed in quotes.

Now check out this code.
1 var originalNum = 23;
2 var newNum = originalNum + 7;
Within the code above in the second statement, JavaScript substitutes the amount 23 when it encounters the variable originalNum. It adds 7 to 23. It assigns the result, 30, to the variable newNum. JavaScript also can handle an expression made from nothing but variables. For example…
1 var originalNum = 23;
2 var numToBeAdded = 7;
3 var newNum = originalNum + numToBeAdded;
A variable is often utilized in calculating its own new value.
1 var originalNum = 90;
2 originalNum = originalNum + 10;
If you enclose variety in quotation marks and add 7…
1 var originalNum = “23”;
2 var newNum = originalNum + 7;
…it won’t work, because JavaScript can’t sum a string and variety. JavaScript interprets “23” as a word, not variety. within the second statement, it doesn’t add 23 + 7 to a total of 30. It does something which may surprise us. Now, know that variety enclosed by quotation marks isn’t variety, but a string, and JavaScript
can’t do an addition thereon.

Note that any particular variable names are often the name of a variety of variables or string variables. From JavaScript’s point of view, there’s nothing during a name that denotes one quite variable or another. In fact, a variable can start out together sort of variable, then become another sort of variable.
Did we notice what’s new in…
1 var originalNumber = 23;
2 var newNumber = originalNumber + 7;
The statement assigns to the variable newNumber the results of a mathematical process. The results of this operation, of course, maybe a number value. The example mixes a variable and a literal number during a math expression. But we’ll too use nothing but numbers or nothing but variables. It’s all equivalent to JavaScript.We simply can’t begin a variable name with variety. The statement…
var 1stPresident = “Washington”; …is illegal, because of that initial “1” within the variable name. But we’ll include numbers in variable names, as long as none of them come first. The statement… var prezWhoCame1st = “Washington”;
…is legal. Conveniently, if we specify variety rather than a string as an alert message… alert(144);
…or if we specify a variable that represents variety as an alert message…
1 var caseQty = 144;
2 alert(caseQty);

Now, JavaScript automatically converts it to a string and displays it.

Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 421

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top