HOLLA TECH - LEARN

Variables

Variables are containers for storing data values. The value of a variable can change throughout the program.
Use the var keyword to declare a variable:

var x = 10

In the example above, the value 10 is assigned to the variable x.

NOTE!
JavaScript is case sensitive. For example, the variables lastName and lastname, are two different variables.

The Equal Sign

In JavaScript, the equal sign (=) is called the “assignment” operator, rather than an “equal to” operator.
For example, x = y will assign the value of y to x.

NOTE!
A variable can be declared without a value. The value might require some calculation, something that will be provided later, like user input. A variable declared without a value will have the value undefined.

Using Variables

Let’s assign a value to a variable and output it to the browser.

var x = 100;
document.write(x); 

Result:  

Holla Tech - Learn JavaScript

Using variables is useful in many ways. You might have a thousand lines of code that may include the variable x. When you change the value of x one time, it will automatically be changed in all places where you used it.

NOTE!
Every written “instruction” is called a statement. JavaScript statements are separated by semicolons.

Naming Variables

JavaScript variable names are case-sensitive.
In the example below we changed x to uppercase:

var x = 100;
document.write(X); 

This code will not result in any output, as x and X are two different variables.

Naming rules:
– The first character must be a letter, an underscore (_), or a dollar sign ($). Subsequent characters may be letters, digits, underscores, or dollar signs.
– Numbers are not allowed as the first character.
– Variable names cannot include a mathematical or logical operator in the name. For instance, 2*something or this+that;
– JavaScript names must not contain spaces.

NOTE!
Hyphens are not allowed in JavaScript. It is reserved for subtractions.

Naming Variables
 

There are some other rules to follow when naming your JavaScript variables:

  • You must not use any special symbols, like my#num, num%, etc.
  • Be sure that you do not use any of the following JavaScript reserved words.

Holla Tech - Learn JavaScript

NOTE!
When you get more familiar with JavaScript, remembering these keywords will be much easier.

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE JAVASCRIPT MAIN COURSE PAGE. 

JAVASCRIPT MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT