Use of Switch Case in JavaScript | Use of JavaScript in HTML file

Use of Switch Case in JavaScript | Use of JavaScript in HTML file

 

In this article, you will get information about the use of switch case in JavaScript. This is also a very important chapter of JavaScript. If you want to become a successful JavaScript programmer then you must know about this chapter.




How to use switch case in JavaScript?

Do you want to become a successful web designer?

If you have these questions then you should read this article till the end. In this article, you will find that how to use switch case in JavaScript with an example. I hope you will love this article.

If you want to boost your JavaScript programming language then you must read this book. The book is given below.





You must read these articles to improve your JavaScript skills.

Introduction of JavaScript

Control Statements of JavaScript

Now let’s talk about the use of switch case in JavaScript.

  • Here we use switch keyword.
  • Write an expression for the switch to case.
  • The expression compares with all cases and when the case matches then the code will execute.
  • After a case, we write break keyword.
  • At the end, we write a default case and write some code in default.




Switch(expression){

                Case n1:

                                //code here

                                Break;

                Case n2:

                                //code here

                                Break;

                Case n3:

                                //code here

                                Break;

                Default:

                                //code here

                }




Example:

<!DOCTYPE html>

<html>

<head>

	<title>Switch Case in JavaScript</title>

</head>

<body>

<script type="text/javascript">

	var x=1+1;

	switch(x){

		case 1:

		document.write(x+" is one");

		break;

		case 2:

		document.write(x+" is two");

		break;

		case 3:

		document.write(x+" is three");

		break;

		default:

		document.write(x+" is negative");

		break;

	}

</script>

</body>

</html>




The output of Switch case in JavaScript:

picture of switch case in javascript




Conclusion

In this article, you get information about how to use switch case in JavaScript. Here you got your answer with example and the output of the code. Thank you for reading this article till the end. If you have any questions regarding this article then you should write down it in the comment section below.

1 thought on “Use of Switch Case in JavaScript | Use of JavaScript in HTML file

Leave a Reply

Your email address will not be published. Required fields are marked *