JavaScript for Beginners - Basic Concepts (Variables) (P: V)
Variables
Variables are containers for sorting data values. The value of a variable can change throughout the program.
In JavaScript use the var keyword to declare a variable:
In the example above, the variable x is assigned the value
10.
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.
Let’s assign a value to a variable and output it to the
browser
Result:
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 variable 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
Here are some naming rules for JavaScript variables
-
The first character must be a latter, 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.
-
JavaScript names must
not contain spaces.
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 of the following
JavaScript reserved words
Reserved Words in JavaScript |
|||
abstract |
else |
instanceof |
switch |
boolean |
enum |
int |
synchronized |
break |
export |
interface |
this |
byte |
extends |
long |
throw |
case |
false |
native |
throws |
catch |
final |
new |
transient |
char |
finally |
null |
true |
class |
float |
package |
try |
const |
for |
private |
typeof |
continue |
function |
protected |
var |
debugger |
goto |
public |
void |
default |
if |
return |
volatile |
delete |
implements |
short |
while |
do |
import |
static |
with |
double |
in |
super |
|
Note: When you get more familiar with JavaScript remembering these keywords will be much easier.
Hyphens are not allowed in JavaScript. It is reserved
for subtraction.
Comments
Post a Comment