Java Script
Array, Function and Objects
Example:1
<!DOCTYPE html>
<html>
<head>
<title>JS Fuction arguments Example</title>
</head>
<body>
<script type="text/javascript">
// program to add two numbers using a function
// declaring a function
function add(a, b)
{
document.write(a+b);
}
// calling functions
add(3,8);
add(2,9);
</script>
</body>
</html>
Example:2
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<script type="text/javascript">
var emp=new Object()
emp.id=101;
emp.name="james";
document.write(emp.id+""+emp.name);
</script>
</body>
</html>
Example:3
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<script type="text/javascript">
/*
document.write("</br>"+Number.NEGATIVE_INFINITY);
var temp=new Boolean(false)
document.write("<br>"+"the value is" +temp.toString());
// document.write(temp.toString());
document.write("<br>");
var s1="hello";
var s2="peter";
document.write("first string"+s1+"<br"> "second string"+s2);
document.write("<br>");
document.write("output:"+s1.concat(s2)+"<br>");
document.write("</br>" +Math.floor(2.56));
document.write("</br>" +Math.max(2,5));
document.write("</br>" +Math.sqrt(64));
var large=Number.MAX_VALUE;
var small=Number.MIN_VALUE;
document.write("<br> the largest number is"+ large.toString(2));
document.write("<br> the smallest number is"+ small.toString(8));
var num=Number(15);
document.write("<br> the largest number is"+ num.toString(2));
document.write("<br> the smallest number is"+ num.toString(3));
//var n=new Number(45);
//var Number=number_value;
var currdate=new Date();
//currdate=currdate.toString();
//document.write("<br>"+currdate);
document.write("<br>"+currdate.getDate());
var mydate=new Date("1 jan 2021");
currdate.setDate(25);
document.write("<br>"+currdate);
var string1,string2;
string1=" IT Department " ;
string2=" GECBVN ";
string3=string1+""+string2;
document.write("<br>"+string3);
*/
var string1=new String("GECBVN");
var string2="GECBVN";
var a="hello world";
document.write("<br>"+a.substr(2,2));
var mystr="hello";
var len;
len=mystr.length;
document.write("<br>" +len);
s1="department";
s2="DEPARTMENT";
document.write("output" +s1.toUpperCase()+"<br>");
document.write("output" +s2.toLowerCase()+"<br>");
document.write(Math.min(4,5,6,7)+"<br>");
document.write(Math.sqrt(4)+"<br>");
document.write(Math.pow(4,2)+"<br>");
var mydate=new Date();
document.write("the date is " +mydate.toString()+"</br>")
document.write("the date is " +mydate.getDate()+"</br>")
document.write("the date is " +mydate.getMonth()+"</br>")
document.write("the date is " +mydate.getTime()+"</br>")
document.write("the date is " +mydate.getHours()+"</br>")
document.write("the date is " +mydate.getMinutes()+"</br>")
document.write("the date is " +mydate.getSeconds()+"</br>")
alert("Welcome");
/*
var b="Department";
document.write(b.charAt(2));
var str,result;
str="javascript";
result=str.search("script");
document.write("<br>"+result);
var var1= 1 +'2';
document.write("</br>" +typeof(var1));
document.write("</br>" + var1);
document.write("</br>" + Math.sin("12.45"));
var bestpalces= new Array(5);
bestpalces[0]="Bhavnagar";
bestpalces[1]="Gandhinagar";
bestpalces[2]="Patan";
bestpalces[3]="Surat";
bestpalces[4]="Rajkot";
//bestpalces.push("Aanand");
bestpalces.pop("Rajkot");
document.write("</br>"+bestpalces.pop());
for(i=0;i<bestpalces.length;i++)
{
document.write("</br>" +bestpalces[i]);
}*/
</script>
</body>
</html>
Example:4
<!DOCTYPE html>
<html>
<head>
<title>JS Function Example</title>
<script type="text/javascript">
/* How to Create a Function in JavaScript
Use the keyword function followed by the name of the function.
After the function name, open and close parentheses.
After parenthesis, open and close curly braces.
Within curly braces, write your lines of code.*/
function demo(str1,str2)
{
str="function is"+ str1+""+str2;
return str;
}
</script>
</head>
<body>
<script>
document.write("<h2>" +demo("hello","How are you!")+"</h2>");
</script>
</body>
</html>
Example:5
<!DOCTYPE html>
<html>
<head>
<title>JS Example</title>
</head>
<body>
<script type="text/javascript">
//Function with Arguments
//You can create functions with arguments as well. Arguments should be specified within parenthesis
var count = 0;
function countVowels(name)
{
for (var i=0;i<name.length;i++)
{
if(name[i] == "a" || name[i] == "e" || name[i] == "i" || name[i] == "o" || name[i] == "u")
count = count + 1;
}
document.write("Hello " + name + "!!! Your name has " + count + " vowels.");
}
var myName = prompt("Please enter your name");
countVowels(myName);
</script>
</body>
</html>