Predefined Objects and control statements -Examples
Predefined Objects and control statements -Examples
html
<html>
<head>
<script>
var a,b,c,d;
document.write("<h1>Before Initialization<br><h1>");
document.write("<br><b>Type of a is </b>",typeof a);
document.write("<br><b>Type of b is </b>",typeof b);
document.write("<br><b>Type of c is </b>",typeof c);
a=10;
b=3.14;
c="matrusri";
d=true;
document.write("<h1>After Initialization<br><h1>");
document.writeln("Type of a is ",typeof a);
document.writeln("<br>Type of b is ",typeof b);
document.writeln("<br>Type of c is ",typeof c);
document.writeln("<br><b>Type of d is</b> ",typeof d);
document.writeln("<br>Type of 20 is ",typeof 20);
var marks=[90,100,85,75];
var student={name:"mecs",rollno:100};
document.write("<br>marks of oth index ",marks[0]);
document.write("<br>name is ",student.name);
var s1,s2;
s1="Hello";s2='MECS "Student"';
document.write("<br>",s1);
document.write("<br>",s2);
</script>
</head>
</body>
NumberObject.html
<html>
<head><title>Nummethods</title></head>
<body>
<h1>NUMBER-METHODS</h1>
<script>
var x=15;
var n=5.34567;
var y=new Number(15);
var z=new Number(15);
var s=x.toString();
var e=x.toExponential(3);
var f=n.toFixed(3);
var p=x.toPrecision(4);
document.writeln("type of e "+typeof e+"<br>");
document.writeln("type of f "+typeof f+"<br>");
document.writeln("type of p "+typeof p+"<br>");
document.writeln("Exponential form"+e+"<br>");
document.writeln("tofixed function "+f+"<br>");
document.writeln("toPrecision function"+p+"<br>");
document.writeln("<h1>Number Conversion Methods</h1>"+"<br>")
var a=Number(e);
document.writeln("type of a "+typeof a+"<br>");
var b=parseInt(f);
document.writeln("type of b "+typeof b+"<br>");
var c=parseFloat(n);
document.writeln("type of c "+typeof c+"<br>");
</script>
</body></html>
Number_Properties.html
<html>
<head><title>properties</title>
</head>
<body>
<h1>Number Properties</h1>
<script>
document.writeln("MIN-VALUE "+Number.MIN_VALUE+"<br>");
document.writeln("MAX-VALUE "+Number.MAX_VALUE+"<br>");
document.writeln("NEGATIVE-INFINITY "+Number.NEGATIVE_INFINITY+"<br>");
document.writeln("POSITIVE-INFINITY "+Number.POSITIVE_INFINITY+"<br>");
document.writeln("Not a Number "+Number.NaN+"<br>");
</script>
</body>
</html>
String_Object.html
<html>
<head><title>string</title>
</head>
<body>
<h1 style="color:green">STRINGS- STRING OBJECT- STRING METHODS</h1><h3>
<script type="text/javascript">
s1='Welcome to Strings'
s2="hello"
s3='This is "special"'
<html>
<head><title>Numbers</title></head>
<body>
<h1 style="color:red">MATH PROPERTIES</h1>
<script>
document.writeln(Math.E+"<br>");
document.writeln(Math.PI+"<br>");
document.writeln("<h1>METHODS IN MATH OBJECT</h1>")
document.writeln(" max(1,6,10,3) "+Math.max(1,6,10,3)+"<br>");
document.writeln(" min(1,6,10,3) "+Math.min(1,6,10,3)+"<br>");
document.writeln(" cbrt(27) "+Math.cbrt(27)+"<br>");
document.writeln(" floor(6.4) "+Math.floor(6.4)+"<br>");
document.writeln(" abs(-10) "+Math.abs(-10)+"<br>");
</script>
</body>
</html>
Output:
Dateobject.html
<html>
<head></head>
<body>
<center><h1>GET METHODS IN DATE OBJECT</h1>
<script language="javascript">
var d=new Date();
document.writeln(d+"<br>");
document.writeln("YEAR= "+d.getFullYear()+"<br><br>"); //current
document.writeln("MONTH= "+d.getMonth()+"<br><br>"); //0 to 11
document.writeln("Date= "+d.getDate()+"<br><br>"); // 1 to 31
document.writeln("Day= "+d.getDay()+"<br></br>"); //0 to 6
document.writeln("HOURS ="+d.getHours()+"<br><br>"); //0 to 23
document.writeln("MINUTES="+d.getMinutes()+"<br><br>"); // 0 to 59
document.writeln("SECONDS ="+d.getSeconds()+"<br><br>"); //0 to 59
document.writeln("MILLISECONDS "+d.getMilliseconds()+"<br><br>");//0 to 999
document.writeln("TIME ="+d.getTime()+"<br>");
</script>
</center>
</body>
</html>
Output:
CONTROL STATEMENTS:
Ifelse.html
<html>
<head>
<script>
function age() {
var inputAge = parseInt(document.getElementById('t1').value); // Get the value from the input
if (inputAge >= 18) {
window.alert("Eligible for Vote"); // Show alert if age >= 18
} else {
document.getElementById("res").innerHTML="Not eligible for Vote"
// Show alert if age < 18//alert("Not eligible for Vote" );
}
}
</script>
</head>
<body>
Enter age:<h1><input type="text" id="t1" name="age"><br></h1>
<button type="button" id="t2" onclick="age()">Verify</button>
<p id="res"></p>
</body>
</html>
Output:
Elseifladder.html
<html>
<head>
<script>
function relation(){
if(document.getElementById("t1").value==document.getElementById("t2").value)
{
document.getElementById("res").innerHTML="Both are Equal" //alert("both are equal")
}
else if(document.getElementById("t1").value>document.getElementById("t2").value)
{
document.getElementById("res").innerHTML="first number is greatest"
}
else
document.getElementById("res").innerHTML="2nd number is greatest"
}
</script>
</head>
<body>
<h1>Greatest of two numbers</h1>
<h1>Enter first number:<input type="text" id="t1" ><br></h1>
<h1>Enter second number:<input type="text" id="t2" ><br></h1>
<button type="button" name="b" value="check" onclick="relation()">check</button>
<div id="res"></div>
</body></html>
Output:
Switch.html
<html>
<head>
<script type="text/javascript">
function pg() {
var obtainedMarks = parseInt(document.getElementById("om").value);
var maxMarks = parseInt(document.getElementById("mm").value);
var percentage = ((obtainedMarks / maxMarks) * 100);
document.getElementById("perc").value = percentage;
var g;
Alert.html
<!DOCTYPE html>
<html>
<head>
<script>
</script>
</head>
<body>
<button onclick='alert("This is an alert message!")'>Click me for Alert</button>
</body>
</html>
Output:
Confirm.html
<!DOCTYPE html>
<html>
<head>
<script>
function showConfirm() {
var result = confirm("Are you sure you want to proceed?");
if (result) {
alert("You clicked OK!");
} else {
alert("You clicked Cancel!");
}
}
</script>
</head>
<body>
<button onclick="showConfirm()">Click me for Confirm</button>
</body>
</html>
Output:
Prompt.html
<!DOCTYPE html>
<html>
<head>
<script>
function showPrompt() {
var name = prompt("Please enter your name:", "Your Name");
if (name != null && name != "") {
alert("Hello " + name + "! Welcome.");
} else {
alert("No name entered.");
}
}
</script>
</head>
<body>
<button onclick="showPrompt()">Click me for Prompt</button>
</body>
</html>
Output: