Javascript
Javascript
document.getElementById("demo").innerHTML="Hello Javascript";
Javascript Operators:
1) Arithmetic operator:
+ additon
- subtration
* multiplication
/ division
% modulus x%10
++ Increment x++; x=x+1
-- Decrement
** raise to operation a**2
Assignment Operator:
= x=y
+= x+=y x=x+y
-= x-=y x=x-y
*=
/=
%=
<<=
>>=
<<<=
&=
^=
!=
**=
Comparison Operator:
== equal to
=== equal to value and equal to type.
x=10
y="10"
x==y true
x===y false
!=
!==
>
<
<=
>=
Logical Operator:
&& and
|| or
! not
String Operator:
Conditional(Ternary Operator)
syntax:
variablename=(condition)?value1:value2;
ex:
var voteable=(age<18)?"to young":"old enough";
bitwise operator:
Example:
Javascript loops:
if you want the same code again and again.
1) for
2)for/in
3)for/of
4)while
5)do/while
1) for
synatx:
for(stmt1;stmt2,stmt3)
{}
ex:
var i;
for(i=0;i<3;i++)
{
statement..
}
2) for/in
(loop through properties of any object)
var person={fname:"abc",lname:"xyz",age:25};
var text="";
var x;
for(x in person)
{
text+=person[x];
}
syntax:
while(condition)
{
statement;
}
ex:
while(i<10)
{
text+=i;
i++;
}
5)do/while loop
do
{
statement
}while(condition);
if ---else:
switch:
try---catch
1) Alert Box
2) Confirm Box
3) Prompt Box
1) Alert Box:
An alert box is used if you want to make sure information comes through the
user.
Syntax:
window.alert("Welcome to CS");
alert("Welcome to CS");
When an alert boc pops up the user will have to click ok to proceed further.
2) Confirm Box:
Syntax:
3) Prompt box:
syntax:
window.prompt("ENter a number");
prompt("sometext");
ok : True
cancel: False
Javascript Functions:
syntax:
function funtion_name(parameter1,parameter2..)
{
//code to be executed
}
Function Invocation
1)when an event occure
2) invoke through javascript
3) automatically (self invoked)
Function Return:
when javascript reaches a return statement the function will stop executing
EX:
function myFunction(a,b)
{
var c= a*b;
return a*b;
}
var x=myFunction(4,3);
Javascript Events:
Timing Events:
The window object allows execution of code at specified time intervals. These
time intervals are called Timing Events.
setTimeout(function,milliseconds)
function:function to be executed
milliseconds: number of milliseconds before execution.
Execute a function after waiting sepecified number of milliseconds.
clearTimeout(function variable_name);