Programs
Programs
#advanced
1.call
//Calls (executes) a function and sets its this to the provided value, arguments can be passed as they are.
var firstName=
name:"Thomas",
sayHello:function(name)
console.log(name+" "+this.name);
var lastName=
name:"Edison"
firstName.sayHello.call(lastName,"Thomas");
2.closure
// A closure is an inner function that has access to the outer (enclosing) function’s variables—scope
chain.
//The closure has three scope chains (its own scope,outer function’s variables,global variables).
function showName(firstName,lastName)
// this inner function has access to the outer function's variables, including the parameter.
function makeFullName()
return makeFullName();
showName("michael","action");
3.global scope
// A global variable has global scope means all scripts and functions on a web page can access it.
var Name="Michel";
getName();
function getName()
4.bind
// If we assign a method that uses “this” to a variable, then “this” will have value of the global data array,
and not the data array of the user object.
var sum=function(a, b)
return a + b;
};
console.log(add5(10));
5.local variabel
function writeName()
writeName();
6.objects
var person = {
firstname : "Bob",
lastname : "Marley",
age : 50
};
7.this
//this keyword gets the value of the object that invokes the function where this is used.
var person={
firstName:"Harry",
lastName:"Edison",
fullName:function()
{
console.log(this.firstName + " " + this.lastName);
person.fullName();
lastName = "Edison";
function showFullName ()
//"this" inside this function will have the value of the window object
var person =
firstName :"Mary",
lastName :"Thomas",
showFullName : function ()
{
#basic
1.calling function
<!DOCTYPE html>
<html>
<body>
<h1>Calling a function</h1>
<p>
<script>
function myFunction() {
document.getElementById("myPar").innerHTML = "Hello";
</script>
</body>
</html>
2.apply
// The apply() method allows to borrow functions /switch the context and set the this value in function
invocation.
var person=
firstName :"Harry",
lastName :"Edison",
showFullName :function()
//The "context"
};
//when we invoke showFullName() on the person object, the context becomes person object.
//another object
var anotherPerson=
firstName :"Mary",
lastName :"Thomas"
};
//By using apply() method, "this" gets the value of whichever object invokes the "this" function
person.showFullName.apply(anotherPerson);//Mary Thomas
<!DOCTYPE html>
<html>
<body>
</script>
</body>
</html>
4.hello world
<html>
<body>
<script>
document.write("Hello World");
</script>
</body>
</html>
5.Multiline commens
<!DOCTYPE html>
<html>
<body>
<h1 id="myHeader"></h1>
<p id="myPara"></p>
<script>
/*
*/
</script>
<p>
</p>
</body>
</html>
6.singleline commens
<!DOCTYPE html>
<html>
<body>
<h1 id="myHeader"></h1>
<p id="myPara"></p>
<script>
// Write to a heading:
// Write to a paragraph:
</script>
<p>
</p>
</body>
</html>
7.using a variable
<!DOCTYPE html>
<html>
<body>
<script>
var firstname;
firstname = "Goku";
document.write(firstname);
document.write("<br>");
firstname = "Krillin";
document.write(firstname);
</script>
displays the value, changes the value, and displays the value again.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Example</h1>
<script>
document.write("<p>This is a paragraph</p>");
</script>
</body>
</html>
#conditional
<!DOCTYPE html>
<html>
<body>
<p>
<br />
</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = "";
} else {
x = "Good night";
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
2.if example
<!DOCTYPE html>
<html>
<body>
<p>
Click the button to get a "Have a nice day" greeting if the time is
<br />
<p id="demo"></p>
<script>
function myFunction() {
var x = "";
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
3.switch statement
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunction() {
var x;
switch (d) {
case 0:
x = "Today is Sunday";
break;
case 1:
x = "Today is Monday";
break;
case 2:
x = "Today is Tuesday";
break;
case 3:
x = "Today is Wednesday";
break;
case 4:
x = "Today is Thursday";
break;
case 5:
x = "Today is Friday";
break;
case 6:
x = "Today is Saturday";
break;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
#error handling
<html>
<head>
<script>
onerror = handleErr;
alert(txt);
return true;
function message() {
alertMe("Welcome guest!");
}
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script>
function message() {
try {
alertMe("Welcome guest!");
} catch (err) {
alert(txt);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script>
function message() {
try {
adddlert("Welcome guest!");
} catch (err) {
if (!confirm(txt)) {
//action on confirm
</script>
</head>
<body>
</body>
</html>
#function
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
</script>
</head>
<body>
<script>
document.write(myFunction())
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction(a, b) {
return a * b;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script>
function myfunction(txt) {
alert(txt);
</script>
</head>
<body>
<form>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
</script>
</body>
</html>
#loop
1.break statement
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunction() {
var x = "", i = 0;
break;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
2.continue statement
<!DOCTYPE html>
<html>
<body>
<p>Click the button to do a loop which will skip the step where i =
3.</p>
<script>
function myFunction() {
var x = "", i = 0;
if (i == 3) {
continue;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<html>
<body>
<p>
less than 5.
</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = "", i = 0;
do {
i++;
} while (i < 5)
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var person = {
fname : "John",
lname : "Doe",
age : 20
};
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
document.write(fruits[i] + "<br>");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop from 1 to 6, to make HTML headings.</p>
<div id="demo"></div>
<script>
function myFunction() {
var x = "", i;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x = "";
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
8.while loop
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x = "", i = 0;
while (i < 5) {
i++;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
#numbers
1.fibonnaci
<html>
<body>
<script type="text/javascript">
var a = 0, b = 1, c = 0;
document.write("Fibonacci Series<br/>");
document.write("<br/>");
c = a + b;
a = b;
b = c;
</script>
</body>
</html>
2.leap year
<html>
<body>
<script>
function leapYear()
{
var yr=document.getElementById('year').value;
if(x==true)
document.getElementById('res').innerHTML="Leap Year";
else{
</script>
</body>
</html>
3.palindrome
<html>
<body>
<script type="text/javascript">
function Palindrome() {
var i = str.length;
for (var j = i; j >= 0; j--) {
if (str == "") {
} else {
</script>
<form>
Enter a String/Number: <input type="text" id="str" name="string" /><br />
</form>
</body>
</html>
4.prime number
function Prime(max)
if (!temp[i])
primes.push(i);
temp[j] = true;
return primes;
}
Prime(50);
5.random numbers
Math.floor(Math.random() * 10) + 1
<html>
<body>
<script>
a=10;
b=15;
a=a+b;
b=a-b;
a=a-b;
a=10;
b=15;
a=a^b;
b=a^b;
a=a^b;
a=10;
b=15;
a=a*b;
b=a/b;
a=a/b;
/*
Working of Formula :
Browser first evaluates (b=a) expression based on BODMAS rule
*/
a=10;
b=15;
a=b-a+(b=a) ;
</script>
</body>
</html>
#popup
1.alert example
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunction() {
alert("Hello\nHow are you?");
</script>
</body>
</html>
3.confirm example
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunction() {
var x;
} else {
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
4.promt example
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
var x;
if (person != null) {
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>