Table of Contents
hide
Example : How to Print/Display Messages in a Java Script(JS) program?
<html>
<head>
</head>
<body>
<script>
document.write("Hello Inida");
//alert("Hello Inida");
//console.log("Hello India");
</script>
</body>
</html>
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
document.write(" <h2> Hello Inida <h2> ");
</script>
</body>
</html>
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
document.write(" <div style='color:red;'> Hello Inida <div> ");
document.write(" <div style='color:red;'> <h3>Hello Inida</h3> <div> ");
</script>
</body>
</html>
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
document.write("Hello Inida");
document.write("Hello World");
</script>
</body>
</html>
Output:
Hello InidaHello World
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
document.write("Hello Inida");
document.write(" ");
document.write("Hello World");
</script>
</body>
</html>
Output:
Hello Inida Hello World
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
document.write("Hello Inida");
document.write("</br>");
document.write("Hello World");
</script>
</body>
</html>
Output:
Hello Inida
Hello World
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
document.write("Hello Inida </br>");
document.write("Hello World");
</script>
</body>
</html>
Output:
Hello Inida
Hello World
Example : A JS program to Add Two Values and display them.
<html>
<head>
</head>
<body>
<script>
x=20;
y=30;
z=x+y;
document.write(z);
//alert(z);
</script>
</body>
</html>
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
var x=20, y=30, z;
//let x=20, y=30, z;
z=x+y;
document.write(z);
//alert(z);
</script>
</body>
</html>
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
let x=20, y=35, z;
z=x+y;
document.write(z);
//alert(z);
</script>
</body>
</html>
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
let x=20, y=35, z;
z=x+y;
document.write("The sum of " + x + " and " + y + " is = "+z);
//alert(z);
</script>
</body>
</html>
Output :
The sum of 20 and 35 is = 55
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
var x= parseInt(prompt("Enter first value"));
var y= parseInt(prompt("Enter second value"));
z=x+y;
document.write("The sum of " + x + " and " + y + " is = "+z);
//alert(z);
</script>
</body>
</html>
Output :
The sum of 20 and 35 is = 55
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
var x= parseInt(prompt("Enter first value", "Enter Numeric value here"));
var y= parseInt(prompt("Enter second value", "Enter Numeric value here" ));
z=x+y;
document.write("The sum of " + x + " and " + y + " is = "+z);
//alert(z);
</script>
</body>
</html>
Output :
The sum of 20 and 35 is = 55
-------------------- OR --------------------
<html>
<head>
</head>
<body>
<script>
var x= parseFloat(prompt("Enter first value", "Enter Float value here"));
var y= parseFloat(prompt("Enter second value", "Enter Float value here" ));
z=x+y;
document.write("The sum of " + x + " and " + y + " is = "+z);
//alert(z);
</script>
</body>
</html>
Output:
The sum of 52.35 and 24.52 is = 76.87
Example : A JS program to Swap Two Values without Third Variables and display them.
<html>
<body>
<script>
let x = 5, y = 10;
[x, y] = [y, x];
document.write(x," ",y);
</script>
</body>
</html>
Output:
10 5
Example : A JS program to Count the no. of Digits present in a numeric value and display them.
<html>
<body>
<script>
let num=24153;
let digit = num.toString().length;
document.write(digit);
</script>
</body>
</html>
Output:
5
Example : A JS program to generate Random Values and display them.
<html>
<body>
<script>
document.write(Math.floor(Math.random() * 100) + 1);
</script>
</body>
</html>
Output:
Any value each time such as 40 31 57 64...
0 Comments