How to Change Your Variable

By: Dusty Arlia
Published on September 5, 2012
Last Updated on Friday, July 10, 2015 at 5:54 PM
Total Updates: 5

If you want to assign a value to a variable in JavaScript, use the assignment operator (=) like this:

var name = "Dusty";

If you want to change the variable's value, that's simple. Since the variable was already declared with the var keyword, just leave the var keyword out and assign it a value. If you want to change the value of the name variable, do it like this:

name = "Dusty Arlia";

or you can concatenate a string like this:

name =  name + " Arlia";

If you are dealing with a number, you can assign the number to a variable like this:

var score = 7;

and you can change its value like this:

score = score + 3;
score = score / 2;
score = score * 10;