• Home   /  
  • Archive by category "1"

Javascript Var Multiple Assignment

Multiple variable assignment with Javascript

Posted in Javascript -

About a month ago I posted how it is possible to assign multiple variables with the same value in PHP and have since learned that this is also possible to do with Javascript. This can be useful if initializing multiple variables with the same initial value or if needing to make multiple copies of a value and then manipulate each separately.

Assigning multiple variables

Using the same set sort of examples as in the PHP post, but this time with Javascript, multiple variables can be assigned by using = multiple times on the same line of code like so:

var c = b = a;

The above is a more compact equivilent of this:

var b = a; var c = b;

Here's an example where all three variables are assigned initially with the string "AAA" and then the values of each are written out to the current page using document.write:

var c = b = a = 'AAA'; document.write("a: " + a + "<br />" + "b: " + b + "<br />" + "c: " + c + "<br />");

The resulting output on the page would look like this:

a: AAA b: AAA c: AAA

Any subsequent updates to any of the variables will not affect the other assigned variables. In the next example a, b and c are again initialised with "AAA" and then b is changed to "BBB".

var c = b = a = 'AAA'; b = 'BBB'; document.write("a: " + a + "<br />" + "b: " + b + "<br />" + "c: " + c + "<br />");

The output from this example would be:

a: AAA b: BBB c: AAA

Related posts:

Variables (JavaScript)

  • 3 minutes to read
  • Contributors

In JavaScript, a variable contains a value, such as "hello" or 5. When you use the variable, you refer to the data it represents, for example .

You use variables to store, retrieve, and manipulate values that appear in your code. Try to give your variables meaningful names to make it easy for other people to understand what your code does.

Declaring Variables

The first time a variable appears in your script is its declaration. The first mention of the variable sets it up in memory, so you can refer to it later on in your script. You should declare variables before using them. You do this using the keyword.

If you do not initialize your variable in the statement, it automatically takes on the value .

Naming Variables

JavaScript is a case-sensitive language. This means that a variable name such as myCounter is different from the variable name MYCounter. Variable names can be of any length. The rules for creating legal variable names are as follows:

  • The first character must be an ASCII letter (either uppercase or lowercase), or an underscore (_) character. Note that a number cannot be used as the first character.

  • Subsequent characters must be letters, numbers, or underscores (_).

  • The variable name must not be a reserved word.

    Here are some examples of valid variable names:

Here are some examples of invalid variable names:

When you want to declare a variable and initialize it, but do not want to give it any particular value, assign it the value . Here is an example.

If you declare a variable without assigning a value to it, it has the value . Here is an example.

The value behaves like the number 0, while behaves like the special value (Not a Number). If you compare a value and an value, they are equal.

You can declare a variable without using the keyword in the declaration, and assign a value to it. This is an implicit declaration.

You cannot use a variable that has never been declared.


JavaScript is a loosely typed language, as opposed to strongly typed languages like C++. This means that JavaScript variables have no predetermined type. Instead, the type of a variable is the type of its value. This behavior allows you to treat a value as if it were of a different type.

In JavaScript, you can perform operations on values of different types without causing an exception. The JavaScript interpreter implicitly converts, or coerces, one of the data types to that of the other, then performs the operation. The rules for coercion of string, number, and Boolean values are the following:

  • If you add a number and a string, the number is coerced to a string.

  • If you add a Boolean and a string, the Boolean is coerced to a string.

  • If you add a number and a Boolean, the Boolean is coerced to a number.

    In the following example, a number added to a string results in a string.

Strings are automatically converted to equivalent numbers for comparison purposes. To explicitly convert a string to an integer, use the parseInt function. To explicitly convert a string to a number, use the parseFloat function.

One thought on “Javascript Var Multiple Assignment

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *