Discuss the functionality JavaScript can provide in a webpage with the help of a suitable example code.

JavaScript functionality in a webpage with example code

JavaScript can provide interactive functionality such as form validation, dynamic content updates, event handling, animations, etc.

Example Code:

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>JavaScript Example</title>
<script>
function greetUser() {
const name = document.getElementById(‘username’).value;
alert(‘Hello, ‘ + name + ‘!’);
}
</script>
</head>
<body>

<input type=”text” id=”username” placeholder=”Enter your name”>
<button onclick=”greetUser()”>Greet Me</button>

</body>
</html>

This code asks the user for their name, and when they press the button, it displays a greeting in an alert box.