0% found this document useful (0 votes)
56 views

07 Variables

Variables in JavaScript serve as containers that can store and manipulate data during script execution. Variables are declared with the var statement and can then be assigned values. Variable names are case-sensitive, must begin with a letter or underscore, and can use letters, digits, underscores, and dollar signs. Once declared, variables can have their values reassigned and used in expressions and arithmetic operations throughout the script.

Uploaded by

BaneeIshaqueK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

07 Variables

Variables in JavaScript serve as containers that can store and manipulate data during script execution. Variables are declared with the var statement and can then be assigned values. Variable names are case-sensitive, must begin with a letter or underscore, and can use letters, digits, underscores, and dollar signs. Once declared, variables can have their values reassigned and used in expressions and arithmetic operations throughout the script.

Uploaded by

BaneeIshaqueK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript Variables

Variables are "containers" for storing information.


Do You Remember Algebra From School?
Do you remember algebra from school? !"# y!$# %!&y
Do you remember that a letter 'li(e ) coul* be use* to hol* a value 'li(e ")# an* that you coul* use
the information above to calculate the value of % to be ++?
,hese letters are calle* variables# an* variables can be use* to hol* values '!") or epressions
'%!&y).
JavaScript Variables
As -ith algebra# JavaScript variables are use* to hol* values or epressions.
A variable can have a short name# li(e # or a more *escriptive name# li(e carname.
Rules for JavaScript variable names.
Variable names are case sensitive 'y an* Y are t-o *ifferent variables)
Variable names must begin -ith a letter or the un*erscore character
Note: /ecause JavaScript is case0sensitive# variable names are case0sensitive.
1ample
A variable2s value can change *uring the eecution of a script. You can refer to a variable by its
name to *isplay or change its value.
,his eample -ill sho- you ho-
Declaring '3reating) JavaScript Variables
3reating variables in JavaScript is most often referre* to as "*eclaring" variables.
You can *eclare JavaScript variables -ith the var statement.
var x;
var carname;
After the *eclaration sho-n above# the variables are empty 'they have no values yet).
4o-ever# you can also assign values to the variables -hen you *eclare them.
var x=5;
var carname="Volvo";
After the eecution of the statements above# the variable x -ill hol* the value 5# an* carname -ill
hol* the value Volvo.
Note: 5hen you assign a tet value to a variable# use 6uotes aroun* the value.
Assigning Values to 7n*eclare* JavaScript Variables
8f you assign values to variables that have not yet been *eclare*# the variables -ill automatically be
*eclare*.
,hese statements.
x=5;
carname="Volvo";
have the same effect as.
var x=5;
var carname="Volvo";
Re*eclaring JavaScript Variables
8f you re*eclare a JavaScript variable# it -ill not lose its original value.
var x=5;
var x;
After the eecution of the statements above# the variable -ill still have the value of ". ,he value
of is not reset 'or cleare*) -hen you re*eclare it.
JavaScript Arithmetic
As -ith algebra# you can *o arithmetic operations -ith JavaScript variables.
y=x-5;
z=y+5;
You -ill learn more about the operators that can be use* in the net chapter of this tutorial.

You might also like