javascript_variables

JavaScript variables

Return to JavaScript or variable

“Most useful programs need to track a value as it changes over the course of the program, undergoing different operations as called for by your program's intended tasks.

The easiest way to go about that in your program is to assign a value to a symbolic container, called a variable — so called because the value in this container can vary over time as needed.

In some programming languages, you declare a variable (container) to hold a specific type of value, such as numeric or string. Static typing, otherwise known as type enforcement, is typically cited as a benefit for program correctness by preventing unintended value conversions.

Other languages emphasize types for values instead of variables. Weak typing, otherwise known as dynamic typing, allows a variable to hold any type of value at any time. It's typically cited as a benefit for program flexibility by allowing a single variable to represent a value no matter what type form that value may take at any given moment in the program's logic flow.

Javascript uses the latter approach, dynamic typing, meaning variables can hold values of any type without any type enforcement.

As mentioned earlier, we declare a variable using the var statement — notice there's no other type information in the declaration. Consider this simple program:

var amount = 99.99; amount = amount

The amount variable starts out holding the numeric 99.99, and then holds the numeric result of amount

  • 2, which is 199.98.

The first console.log(..) command has to implicitly coerce that numeric value to a string to print it out.

Then the statement amount = “$” + string(amount) explicitly coerces the 199.98 value to a string and adds a “$” character to the beginning. At this point, amount now holds the string value “$199.98”, so the second console.log(..) statement doesn't need to do any coercion to print it out.

Javascript developers will note the flexibility of using the amount variable for each of the 99.99, 199.98, and the “$199.98” values. Static-typing enthusiasts would prefer a separate variable like amountStr to hold the final “$199.98” representation of the value, because it's a different type.

Either way, you'll note that amount holds a running value that changes over the course of the program, illustrating the primary purpose of variables: managing program state.

In other words, state is tracking the changes to values as your program runs.

Another common usage of variables is for centralizing value setting. This is more typically called constants, when you declare a variable with a value and intend for that value to not change throughout the program.

You declare these constants, often at the top of a program, so that it's convenient for you to have one place to go to alter a value if you need to. By convention, Javascript variables as constants are usually capitalized, with underscores _ between multiple words.”

Fair Use Source: B00V20DQU8

javascript_variables.txt · Last modified: 2024/04/28 03:19 (external edit)