- Forums
- Javascipt
- How To Understan Variables Strings In Javascript
This Page Contains information about How To Understan Variables Strings In Javascript By web hosting in category Javascipt with 0 Replies. [2166], Last Updated: Mon Jun 24, 2024
web hosting
Sat Apr 19, 2008
0 Comments
378 Visits
when i was in high school i remember my algebra teacher talking about this. i understood the concept but never paid much attention to it. until now, i do alot of computer programming, specailly in php and variables are essentials for writing scripts in any language.
A variable represents or holds a value. The actual value of a variable ca time. To understand what a variable is, consider a basic statement that ) alllebra class:
x=2
The letter x is used as the name of the variable. It is assigned a value of 2. to change the value, you simple give x a new assigment.
x=4
now the value of x will be equals to the number 4
The name of the variable stays the same, but now it represents a different Taking the math class example one step further, you probably had to solve a problem like this one;
if x=2, then 3+x=?
To get the answer, you put the value of 2 in place of the variable x in the If the value of x changes, so does the answer to the problem. So, if x=7 ,tI turns into 3+7, and now the result is 10.
Variables in JavaScript are much like those used in mathematics. YOl name, and then assign it values based on your needs. If the value of the variable changes, it will change something that happens withitn the script
why are variables so useful?
using variables offers two benefits
- They can save your time in writing and updating your scripts.
- They can make the purpose of your code clearer.
Variable as time savers
Variables speed up script writing because their values can change. When you assign a value to a variable at the beginning of a script, the rest of the script can simply use the variable in its place. If you decide to change the value later, you need to change the code in only one place--where you assigned a value to the variable--rather than in numerous places.
For instance, suppose that back in math class, you were asked to solve this problem:
If x=2, then 3+x-l+2-x=?
You know that you need to substitute the value of2 for each x that appears, for 3+2-1+2-2=4. Now if the teacher wants you to do this problem again with a different value for x, the whole
problem does not need to be rewritten. The teacher can just give you the following instruction:
Solve the above problem for x=4The longer and more complex the problem gets, the more useful the variable becomes. Rather than rewriting the same thing over and over, you can change one variable to offer an entirely new result.
Variables as Code Clarifiers
Since variables represent something, and you can give them meaningful names, they are often easier to recognize when you read over (and debug) your scripts. If you just add numbers, you may forget what they stand for. For example, consider this line of code:
TotalPrice=2.42+4.33;Here, the numbers could mean almost anything. Instead, you might assign 2.42 as the value of a variable named CandyPrice and 4.33 as the value of a variable named OilPrice:
TotalPrice=CandyPrice+OilPrice;Now, rather than trying to remember the meaning of the numbers, you can see that the script is adding the price of some candy to the price of some oil. This is also useful in debugging, because the meaningful variable names make it easier to spot errors.