Assigning Values

The JavaScript assignment operator (=) assigns a value to a variable. The type of data can be a number (the number of items or the result of a calculation), an object (a string or an object path), or a boolean (true or false).

var Result = 15   // The value 15 is assigned to
                       the variable named Result
var Result = Result + 2 // The variable Result is incremented by 2

The data type can be changed at any time. For local variables, the data type is null or undefined until a value is assigned. Once a value is assigned, the variable’s data type defines whether the + operator concatenates or adds; to add, all values must be numeric. See Concatenating versus Adding for converting a string to a number.

JavaScript is a dynamically typed language. This means that you do not need to specify the data type of a variable when you declare it. Data types are converted automatically as needed during script execution. This allows you to reuse variables with different data types. For example, if you define a variable, such as:

var version = 6.5

Later, you can assign a string value to the same variable:

version = "Brio Intelligence 6.6"

Since JavaScript is dynamically typed, this assignment does not cause an error message.

In expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings. For example, consider the following statements:

// returns The version is 6.5
x = "The version is " + 6.5

In statements that involve other operators, JavaScript does not convert numeric values to strings, for example:

"50" - 5 // returns 45
"60" + 5 // returns 65