Introduction
JavaScript
is the client side scripting language for the Web. Now a days all
modern HTML, ASPX, ASP, JSP, PHP pages are using JavaScript to add
additional client side functionality, validate input, communicate
with web servers and much more.
JavaScript
is very easy to learn. I'm sure you will enjoy it.
Lets
start with something strange about JavaScript language. Try to
understand JavaScript language by yourself.
Here
i have mentioned some of the example, first line is input and second
line is output generated by JavaScript language:
>
parseInt("11", 2)
3
>"hello,
world".replace("hello", "goodbye")
goodbye,
world
>
Boolean("")
false
>
Boolean(234)
true
>
"3" + 4 + 5
345
>
3 + 4 + "5"
75
>
1 === true
false
>
true === true
true
>
1 / 0
Infinity
>
-1 / 0
-Infinity
Main
Data Type of JavaScript :
Type
is building block of any type of language.
- Numbers
- Strings
- Booleans
- Functions
- Objects : Function, Array, Date, RegExp
- Null
- Undefined
Main
Control structures of JavaScript :
JavaScript
IF LOOP
var
name = "kittens";
if
(name == "puppies")
{
name += "!";
}
else if (name == "kittens")
{
name += "!!";
}
else
{
name = "!" + name;
}
name
== "kittens!!"
JavaScript
While Loop
while
(true)
{
// an infinite loop!
}
JavaScript
Do While loop
var
input;
do
{
input = get_input();
}
while (inputIsNotValid(input))
JavaScript
For loop
for
(var i = 0; i < 5; i++) {
// Will execute 5 times
}
JavaScript
Switch Case
switch(action)
{
case 'draw':
drawit();
break;
case 'eat':
eatit();
break;
default:
donothing();
}
JavaScript
logical operator :
var
name = o && o.getName();
var
name = otherName || "default";
var
allowed = (age > 18) ? "yes" : "no";
Objects :
How
to create object in javascript :
var
obj = new Object();
var
obj = {};
How
to assign value and get value to object :
obj.name
= "Simon";
var
name = obj.name;
obj["name"]
= "Simon";
var
name = obj["name"];
obj.for
= "Simon"; // Syntax error, because 'for' is a reserved
word
obj["for"]
= "Simon"; // works fine
var
obj = {
name: "Carrot",
"for": "Max",
details: {
color: "orange",
size: 12
}
}
>
obj.details.color
orange
>
obj["details"]["size"]
12
Arrays :
How
to create array in javaScript :
>
var a = new Array();
>
a[0] = "dog";
>
a[1] = "cat";
>
a[2] = "hen";
>
a.length
3
>
var a = ["dog", "cat", "hen"];
>
a.length
3
>
var a = ["dog", "cat", "hen"];
>
a[100] = "fox";
>
a.length
101
>
typeof a[90]
undefined
Functions :
How
to create function in javaScript:
function
add(x, y) {
var total = x + y;
return total;
}
>
add()
NaN
// You can't perform addition on undefined
>
add(2, 3, 4)
5
// added the first two; 4 was ignored
function
avg() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
sum += arguments[i];
}
return sum / arguments.length;
}
>
avg(2, 3, 4, 5)
3.5
But
it would be nice to be able to reuse the function that we've already
created. Luckily, JavaScript lets you call a function and call it
with an arbitrary array of arguments, using the apply()method of any
function object.
>
avg.apply(null, [2, 3, 4, 5])
3.5
The
second argument to apply() is the array to use as arguments; the
first will be discussed later on. This emphasizes the fact that
functions are objects too.
How
to create anonymous function in javaScript :
var
avg = function() {
var
sum = 0;
for
(var i = 0, j = arguments.length; i < j; i++) {
sum
+= arguments[i];
}
return
sum / arguments.length;
}
find
out value of a and b :
>
var a = 1;
>
var b = 2;
>
(function() {
var
b = 3;
a
+= b;
})();
Custom
objects :
function
Person(first, last) {
this.first
= first;
this.last
= last;
}
Person.prototype.fullName
= function() {
return
this.first + ' ' + this.last;
}
Person.prototype.fullNameReversed
= function() {
return
this.last + ', ' + this.first;
}
Person.prototype
is an object shared by all instances of Person. It forms part of a look-up chain (that has a special name, "prototype chain"):
any time you attempt to access a property of Person that isn't set,
JavaScript will check Person.prototype to see if that property exists
there instead. As a result, anything assigned to
Person.prototype becomes available to all instances of that
constructor via the this object.
Inner
functions
function
betterExampleNeeded() {
var
a = 1;
function
oneMoreThanA() {
return
a + 1;
}
return
oneMoreThanA();
}
If you want to more detail then please follow below link.
No comments:
Post a Comment