JavaScript Resources
 
 
Categories
Misc

beginners php
programmershelp
programmingsite
software directory
Low Cost domains
Mobile phone business
Free stuff


 

Stores

 

 
Data Types

Numbers

In JavaScript all numbers are treated as floating point numbers . JavaScript does support integers , octal numbers , hexadecimal numbers etc but at the lowest level , JavaScript sees all numbers as floating point numbers .

Integers

These are numbers with no fractional parts , they can be positive or negative and they can be decimal , hexadecimal or octal .  The most common type of integers are decimal integers (or base 10 ) . these are numbers ranging from 0 - 9 and cannot have a 0 in front of the numbers . Here are some examples .

valid : 2 , 23 , 900 , 54

invalid : 04 , 0300 , 079

Octal integers (also referred to as base-8 ) must begin with a leading zero and then each digit after the leading zero can be in the range of 0 - 7.

hexadecimal integers (also known as base-16) must begin with 0x or 0X . Each digit following this can be in the range of 0 - 9 and from a - f where a - f is the equivalent of 10 - 15 .

Example

A simple example here

<script language = "JavaScript">
<!--
document.write("45 is " + 45 + " decimal (base 10)");
document.write("<br>045 is " + 045 + " octal (base 8)");
document.write("<br>0x45 is " + 0x45 + " hexadecimal (base 16)");
//-->
</script>

now can you see the importance of not putting 0 or 0x in front of your numbers , the results are different in octal and hexadecimal.


Strings

A string is made up of a number of characters . Strings are declared by placing the characters inside either double quotes ( " " ) or inside single quotes( ' ' ) . When a backslash character(\) appears in a string literal , it escapes the character that follows it this means you can place special characters in the string . ( see our special characters reference )

Boolean

A boolean data type can only have two values either true or false . These are often represented by 1 for true and 0 for false in javaScript . It is sometimes better to think of true or false as on or off or even as yes or no .

null

This is provided by the JavaScript keyword null which represents a condition where no value exists .


Naming Variables

There are some guidelines to follow when naming variables in JavaScript . These are as follows

  • The first character of the name must be a letter or an underscore ( _ )
  • All characters following the first character can be letters , underscore , or digits
  • Letters can be either upper or lower case . Note that JavaScript treats the two cases differently so for example firstname is different from FirstName or FIRSTname .

Assigning Variables

To declare a variable in JavaScript we use the var keyword followed by a variable name . You can put multiple variables using the same var keyword if this is the case you use commas to separate the variable names .

Now that you have defined a variable , you can assign a value to it with the assignment operator (=) . Often the declaration and assignment take place at the same time . If a value is assigned to a variable that has not been declared with the var keyword . JavaScript creates a global variable .

Lets see an example of all this .

<script language = "JavaScript">
<!--
//variable declaration with no assignment
var name ;
//assignment with no use of var keyword
name = "iain";
//variable declaration and assignment combined
var age = 29 , height = 6;
//print details
document.write(name ," is ", age ," and " ,height, " foot " );
//-->
</script>

Which gives this result .

 

Variable Scope

Variables in JavaScript can be either local or global . All variables are global unless they are declared in a function in this case the variable is local to that function . You can have two variables with the same name if one is global and one is local to a function . When you access the variable in the function you access the local variable , from outside the function you are accessing the global variable .

You should always use the var keyword to declare a variable inside a function if you wish it to be a local variable . If you do not JavaScript creates a global variable.

 

 

 

SPONSORS

© 2003 onwards programmershelp