In this post, we will learn how to define a function in JavaScript. JavaScript is a front-end side scripting language. So how we can define a function, we will learn in this post. 


How To Define A Function In JavaScript ?


Define A Function In JavaScript

To define a function in JavaScript, we use  function  keyword then function name, then use a pair of parentheses(for parameters if any), then use curly brackets that are used to write a code.


In this given example we use function to change the text when click on the button.


<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How To Define A Function In JavaScript ?</title>
</head>
<body>

<!-- Text change to CodexRadar -->
<p id="changeText">Google</p>
<button type="button" onclick="myFunction();">Change Text</button>

<script>

function myFunction(){
document.getElementById("changeText").innerHTML = "CodexRadar";
}

</script>
</body>
</html> code-box



Summary

In this post we learned how to define a function in JavaScript. You try to make it yourself. I hope you make. If you face any problem, then comment and tell us. Keep visiting the blog to read more similar posts.

Post a Comment

Previous Post Next Post