Javascript Variables

Javascript Variables explained: 

Javascript Variables: Variables and constants are often referred to in mathematics and physics. Similarly, in Javascript, constants are meant for fixed values whereas variables are the names whose values may change. Simply putting, variables are the data stores for our defined or calculated values. Defining x = 10 would put the value 10 into the data store and whenever the programmer calls for the value of x, the JS would print 10. In Javascript, we may define a variable by giving it a name as shown in the example below:

 

// Here we are declaring a variable

var myName;

//Now we can assign a value to our newly created variable

myName = ‘Johny Lever’;

// we can call the variable anywhere we want in Javascript by simply calling its name:

myName;

Variable may also contain numeric values as in the below example:

var myAge;

myAge = 20;

 

Hint; Don’t use hyphens in the variable names: those are not allowed.

 

Changing the value of a variable:

Javascript allows us to change the value of a variable. Let us look at the below example:

 

var changeName;

changeName = ‘Johny’;

ChangeName = ‘Stevens’;

 

Now calling our variable ‘changeName’ anywhere in the script, we shall get the second value assigned to it.

changeName;

The above call within JS would bring about ‘Stevens’ and not ‘Johny’. 

————————————————————————————————————

Read More to JavaScript Variables:

  1. JavaScript Syntax
  2. JavaScript Operators
  3. JavaScript Data Types
  4. JavaScript Variables

Leave a Reply