JavaScript variables are "containers" for storing information.
* Variable names must begin with a lettervar length = 20 ; var width = 10 ; var area = length * width; document.write(" Area of rectangle is ", area);
A string is sequence of valid characters within a given character set. It is normally used to represent text. A string literal is defined by enclosing it in matching single or double quotes.
var answer="It's alright"; var answer="He is called 'Johnny'"; var answer='He is called "Johnny"';
Numbers represent numeric values. It can be of integer or a floating point.
var w=34; var x = 34.23; var y=123e5; // 12300000 var z=123e-5; // 0.00123
Booleans can only have two values: true or false.
var x=true; var y=false;
An Array is an ordered collection of data values. Array indexes are starting with zero, which means the first item is [0], second is [1], and so on.
var myCars=new Array(); // (add an optional integer myCars[0]="Mercedes"; // argument to control array's size) myCars[1]="Volvo"; myCars[2]="BMW"; document.write(myCars); (OR) var myCars=new Array("Mercedes","Volvo","BMW"); document.write(myCars);
Enables basic storage and retrieval of dates and times.
var dt = new Date(); document.write(dt);
An object is delimited by curly braces. Inside the braces the object's properties are defined as name and value pairs (name : value). The properties are separated by commas.
var person={firstname:"John", lastname:"Doe", id:5566}; (OR) var person={ firstname : "John", lastname : "Doe", id : 5566 }; You can address the object properties in two ways: name=person.lastname; name=person["lastname"];
JavaScript also has two keyword literals that it considers to be objects. These are null and undefined.
Something with the value null has no value, which is to say that it does not contain a valid object, array, string, number, or boolean value. It is the value of no value. Normally you assign the value null to something when you want it to have a valid but non-existent value.Variables can be emptied by setting the value to null;
Unlike null, undefined is not a keyword. It is a predefined global variable, but it is treated like any other variable. An undefined variable is one that has been named, but does not have a value assigned to it. This means that it is not zero, it is notnull, but it is undefined.
cars=null; person=null;
A new variable can be declared using the "new" keyword:
var carname=new String; var x=new Number; var y=new Boolean; var cars= new Array; var person= new Object;
JavaScript has many operators and many types of operators. They can be categorized as
1.Arithmetic operators<html> <body< <script type="text/javascript"> /* ****************************************************************** Example Addition, Subtraction, multiplication operators ****************************************************************** */ var x = 20 ; var y = 10 ; //Addition p = x + y; document.write(" x + y is ", p); document.write("
"); //Subtraction q = x - y; document.write(" x - y is ", q); document.write("
"); //Multiplication r = x * y; document.write(" x * y is ", r); document.write("
"); //Division s = x / y; document.write(" x / y is ", s); document.write("
"); //Modulus x = 10 ; y = 6 ; z = x % y; document.write(" x % y is ", z); document.write("
"); //--> </script> </body> </html>
The some shortcut notations :
Relational operators allow you to test the relationship between two operands.
The equality operator is represented by the double equals sign ( == ) It is used to assess whether two operands have the same value. If they do, then it returns a value of true. Otherwise it returns a value of false.
The equality operator will do data type conversion if necessary. All of the following statements equate to true:
Output :Different arrays are never equal, even if they contain the same information. If you want to test the elements of the arrays for equality, you have to test the individual elements.
There is also an identity operator, represented by a triple equals sign ( === ), which only equates to true when both operands have the same value and are of the same type.
The inequality operator( != returns true where equality would return false. The non-identity operator ( !== ) returns true where identity would return false.
Comparison operators test the relationship, or relative order, of two values.
The only string operator is the concatenation operator. It is represented by the plus sign ( + ). The concatenation is a special case of operator. It is an overloaded operator, since it serves more than once function depending on context. It concatenates strings, but it also serves as the addition operator for numbers.
var a = "Afixi"; var b = "Technologies"; alert(a+b);
2 + '1' // equals '21' 2 - '1' // equals 1
There are three logical operators. Logical operators work with boolean values.
The logical AND (double ampersands) compares two boolean operands and equates to true if they are both true.
The logical OR (double vertical bars) compares two boolean operands and equates to true if either one is true.
The logical NOT (exclamation point) negates the value of boolean operand, which is to say it returns true if the operand has a value of false.
if (2 < 3 && 4 > 3) alert("Hi"); else alert("Hello");
The bitwise operators as their name suggests actually work on individual bits
So what bitwise operators are there and what do they do?
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
voteable=(age<18)?"Too young":"Old enough";
The JavaScript while loop consists of a condition and the statement block.
Having looked at the example, let us take a look at the formal definition of the for loop.
The javascript for ..in and continue used to loop through the elements of an array. It is also used to loop through the elements of the object.
var x; var uscities = new Array(); uscities[0] = "Boston"; uscities[1] = "Chicago"; uscities[2] = "Austin"; for (x in uscities) { document.write(uscities[x] + "
"); }
After editing Click here: |
Output:
|