Some JavaScript code for Practice:
❖ Write a JavaScript program to display a message on a web page.
<html>
<body>
<script>
[Link]("Hello World");
</script>
</body>
</html>
❖ Write a JavaScript program to display a heading and bold text.
<html>
<body>
<script>
[Link]("<h1>JavaScript</h1>");
[Link]("<b>Lab Exam Program</b>");
</script>
</body>
</html>
❖ Write a JavaScript program to display an alert message on button click.
<html>
<body>
<button Me</button>
<script>
function showAlert() {
alert("Button clicked!");
}
</script>
</body>
</html>
❖ Write a JavaScript program to take name from user using prompt() and
display it.
<html>
<body>
<script>
var name = prompt("Enter your name");
[Link]("Welcome " + name);
</script>
</body>
</html>
❖ Add two numbers using prompt() and display the result.
<html>
<body>
<script>
var a = prompt("Enter first number");
var b = prompt("Enter second number");
var sum = parseInt(a) + parseInt(b);
[Link]("Sum = " + sum);
</script>
</body>
</html>
❖ Take text input and display it using button click.
<html>
<body>
<h2>Enter your name</h2>
<input type="text" id="name">
<button > <script>
function showmessage(){
var n = [Link]("name").value;
[Link]("Welcome" + n);
}
</script>
</body>
</html>
❖ Use confirm() method and display the result.
<html>
<body>
<script>
var reply = confirm("You must always follow the traffic rules");
[Link](reply);
</script>
</body>
</html>
❖ Display variable values using JavaScript.
<html>
<body>
<script>
var name = "John";
var age = 12;
[Link]("Name: " + name + "<br>");
[Link]("Age: " + age);
</script>
</body>
</html>
❖ Write a JavaScript program using arithmetic operators.
<html>
<body>
<script>
var a=2
var b=10
[Link](“The addition of two number is=”);
var result=a+b
[Link](“result”);
[Link](“<br>The multiplication of two number is=”);
var result=a*b
[Link](“result)
</script>
</body>
</html>