03. Javascript Variables

Javascript variables
Do you Remember algebra from school ?
JavaScript Variables
Declaring (creating) JavaScript Varibles
Assigning values to undeclared javascript varibles
Redeclaring JavaScript varibles
Javascript Arithmetic
Do you Remember algebra from school ?
x=5,y=6,z=x+y
These letters are called variables.
Variblescan be used to hold values (x=5) or expressions (z=x+y)
javascript varibles
A varible can have a short name, like x, or a more descriptive name, like carname.
Rules for Javascript varible names:
variable names are case sensitive
varible names must begin with a letter ,the underscore character, or a dollar sign.
subsequent characters may be letter, number,underscore,or dollar sign.
javascript is a case sensitive, varibles are case sensitive
<html>
<body.
<script type="text/javascript">
var firstname;
firstname="Hege";
document.write(firstname);
document.write("<br/>");
firstname="Tove";
document.write(firstname);
</script>
<p>The script above decleres a variable, assigns a value to it, display the value,
changes the value and displays  the value agin.</p>
</body>
</html>
Declaring (creating) javascript variables
crating variables in javascript is most often refferred to as "declaring" variables
you can declare javascript variables with the var statement:
var x;
var carname;
After the declaration shown, the variables are empty.
you can also assign valuses to the variables when you declare them:
var x=5;
var carname="volvo";
Note: when you assign a text to a varible, use quotes around the value.
Assigning Values to Undeclared Javascript Varibles
If you assign values to varibles that have not yet been declared, the varibles will automatically be declared.
x=5;
carname="volvo";
Redeclaring Javascript varibles
if you redeclare a javascript variable, it will not lose its original value
var x=5;
var x;
the varible xwill still have the value of 5. the value of x is not reset (or cleared) when you redelare it.
Javascript Arithmetic
As with algebra, you can do arithmetic operations with javascript variables:
y=x-5;
z=y+5;
sometimes the result seem unpredictable.
if at least one varible on the right side of an assignment expession contains a string value,
the result will be a string and the "+" operator is applied as the concatenation operator to the toString() values of the variables.
only if all the variables to the right of the  assignment operator are numbers will the result be a number.

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams