What is JavaScript
JavaScript is a lightweight, interpreted programming language. It is commonly used to create dynamic and
interactive elements in web applications. JavaScript is very easy to implement because it is integrated with HTML. It
is open and cross-platform.
Why to Learn JavaScript?
JavaScript is a MUST for students and working professionals to become a great Software Engineer, especially when
they are working in Web Development Domain. We will list down some of the key advantages of learning JavaScript−
JavaScript is the most popular programming language in the world, making it a programmer's great choice. Once you
learn JavaScript, it helps you develop great front-end and back-end software using different JavaScript based
frameworks like jQuery, [Link], etc.
JavaScript is everywhere, it comes installed on every modern web browser and so to learn JavaScript, you really do
not need any special environment setup. For example, Chrome, Mozilla Firefox, Safari, and every browser you know
as of today, supports JavaScript.
JavaScript helps you create really beautiful and crazy fast websites. You can develop your website with a console like
look and feel and give your users the best Graphical User Experience.
JavaScript usage has now extended to mobile app development, desktop app development, and game development.
This opens many opportunities for you as JavaScript Programmer.
Due to high demand, there is tons of job growth and high pay for those who know JavaScript. You can navigate over
to different job sites to see what having JavaScript skills looks like in the job market.
Great thing about JavaScript is that you will find tons of frameworks and Libraries already developed which can be
used directly in your software development to reduce your time to market.
JavaScript is in all over the world, and companies like Google, Meta, Microsoft, PayPal, LinkedIn, etc. also use
JavaScript.
Furthermore, JavaScript has more than 1.5 lakh libraries. It is also growing.
A huge community of JavaScript is available on the internet with students, developers, and mentors. So anyone can
easily get support.
There could be 1000s of good reasons to learn JavaScript Programming. But one thing for sure, to learn any
programming language, not only JavaScript, you just need to code, and code, and finally code until you become an
expert.
JavaScript Applications
As mentioned before, JavaScript is one of the most widely used programming languages (Front-end as well as Back-
end). It has its presence in almost every area of software development. I'm going to list few of them here:
Client side validation − This is really important to verify any user input before submitting it to the server and
JavaScript plays an important role in validating those inputs at front-end itself.
Manipulating HTML Pages − JavaScript helps in manipulating HTML page on the fly. This helps in adding and deleting
any HTML tag very easily using JavaScript and modify your HTML to change its look and feel based on different
devices and requirements.
User Notifications − You can use JavaScript to raise dynamic pop-ups on the webpages to give different types of
notifications to your website visitors.
1
Back-end Data Loading − JavaScript provides Ajax library which helps in loading back-end data while you are doing
some other processing. This really gives an amazing experience to your website visitors.
Presentations − JavaScript also provides the facility of creating presentations which gives website look and feel.
JavaScript provides RevealJS and BespokeJS libraries to build a web-based slide presentation.
Server Applications − Node JS is built on Chrome's JavaScript runtime for building fast and scalable network
applications. This is an event based library which helps in developing very sophisticated server applications including
Web Servers.
Machine learning − Developers can use the [Link] library to complete the task related to machine learning.
Game Developments − JavaScript contains multiple libraries and NPM packages to design graphics for the game. We
can use HTML, CSS, and JavaScript with libraries to develop the games.
Mobile applications − We can use frameworks like React Native to build feature-rich mobile applications.
Internet of Things (IoT) − JavaScript is used to add functionality to devices like smartwatches, earbuds, etc.
Data visualization − JavaScript contains the libraries like [Link] to visualize the data efficiently. The [Link] is also used
to prepare high-quality charts to visualize the data.
Cloud computing − We can use JavaScript in serverless computing platforms like Cloudflare and AWS lambda to write
and deploy functions on the cloud.
This list goes on, there are various areas where millions of software developers are happily using JavaScript to
develop great websites and other software.
Basic JavaScript Example
Below is a basic JavaScript example that will print "Getting started with JavaScript tutorial" on the webpage. You can
edit this example and execute it by clicking on the "Edit & Run" button.
<script>
[Link]("Getting started with JavaScript tutorial");
</script>
JavaScript Features
JavaScript is a highly popular and widely-used programming language for web development. It has a variety of
features that make it powerful and flexible. Some of these features include being dynamic, lightweight, interpreted,
functional, and object-oriented.
Many open-source JavaScript libraries are available, facilitating the utilization of JavaScript in both frontend as well
as backend development. Let's highlight some of the key features of JavaScript.
Easy Setup
We dont need a particular editor to start writing the JavaScript code. Even anyone can write JavaScript code in
NotePad. Also, JavaScript can be executed in the browser without any interpreter or compiler setup.
You can use the <script > tag to add JavaScript in the HTML file. However, it also allows you to add JavaScript to the
web page from the external JavaScript file, having '.js' extension.
Browser Support
2
All browsers support JavaScript, as all modern browser comes with the built-in JavaScript execution environment.
However, you can also use the window object to check whether the browser supports JavaScript or its particular
feature.
Dom Manipulation
JavaScript allows developers to manipulate the webpage elements. Also, you can control the browser.
It contains the various methods to access the DOM elements using different attributes and allows to customize the
HTML elements.
Event Handling
JavaScript allows you to handle the events used to interact with the web page.
For example, you can detect the mouse click on a particular HTML element using JavaScript and interact with the
HTML element.
Some other events also exist, like detecting the scrolling behavior of web page, etc. We will explore all events in the
JavaScript events chapter.
Dynamic Typing
JavaScript decides the type of variables at runtime. So, we dont need to care about variable data type while writing
the code, providing more flexibility to write code.
Also, you can assign the values of the different data types to a single variable. For example, if you have stored the
number value of a particular variable, you can update the variables value with the string.
Functional Programming
JavaScript supports the functional programming. In JavaScript, you can define the first-class function, pure functions,
closures, higher-order functions, arrow funcitons, function expresions, etc.
It mostly uses the functions as a primary building blocks to solve the problem.
Cross-platform Support
Each operating system and browser support JavaScript. So, it is widely used for developing websites, mobile
applications, games, desktop applications, etc.
Object-oriented Programming
JavaScript contains the classes, and we can implement all object-oriented programming concepts using its
functionality.
It also supports inheritance, abstraction, polymorphism, encapsulation, etc, concepts of Object-oriented
programming.
Built-in Objects
JavaScript contains built-in objects like Math and Date. We can use a Math object to perform mathematical
operations and a Date object to manipulate the date easily.
However, you can also manipulate the functionality of the built-in object.
3
Object Prototypes
In JavaScript, everything is an object. For example, array, function, number, string, boolean, set, map, etc. are
objects.
Each object contains the prototype property, which is hidden. You can use the prototype property to achive
inheritance or extend the functionality of class or object, by other objects functionality.
Global Object
JavaScript contains the global object to access the variables which are available everywhere.
To access global variables in the browser, you can use the window object, and in [Link], you can use the 'global'
keyword to access global variables.
Recently, globalThis keyword is introduced to access the global variables, and which is supported by the most
runtime environments.
Built-in Methods
JavaScript also contains the built-in methods for each object. Developers can use the built-in methods to write
efficient and shorter codes.
For example, the Array object contains the filter() method to filter array elements and the sort() method to sort the
array. The String object contains the replace() method to replace text in the string, the trim() method to remove
whitespaces from the string, etc.
Modular Programming
JavaScript allows you to write the code in different modules and connect them with the parent module. So
developers can write maintainable code.
By writing the code in a separate module, you can reduce the complexity of the code and reuse each module
whenever you require.
JSON
JSON stands for JavaScript object notation. It is a widely used data format to exchange data between two networks.
For example, server and client.
JavaScript also supports the JSON format to store the data.
Asynchronous Programming
JavaScript is a single-threaded programming language. To execute your code faster, you can
use asynchronous programming.
You can use promises in JavaScript to write asynchronous code, allowing us to handle multiple tasks asynchronously.
Event-driven Architecture
The event-driven architecture of JavaScript allows developers to create interactive and responsive web applications
by handling a large user base concurrently.
4
Due to the vast features and applications of JavaScript, the front end of Facebook is built on JavaScript. Netflix is
built using the ReactJS framework of JavaScript. Similarly, Amazon, PayPal, Airbnb, LinkedIn, Twitter, etc., are also
built using JavaScript.
Server-side Support
The [Link] runtime environment of JavaScript is widely used to create the backend of the application, as javaScript
can also be used to create servers. It allows you to create a scalable backend for the application.
JavaScript Placement in HTML File
There is flexibility to place JavaScript code anywhere in an HTML document. However, the most preferred ways to
include JavaScript in an HTML file are as follows −
Script in <head>...</head> section.
Script in <body>...</body> section.
Script in <body>...</body> and <head>...</head> sections.
Script in an external file and then include in <head>...</head> section.
You can follow the syntax below to add JavaScript code using the script tag.
<script>
// JavaScript code
</script>
In the following section, we will see how we can place JavaScript in an HTML file in different ways.
JavaScript in <head>...</head> section
If you want to have a script run on some event, such as when a user clicks somewhere,then you will place that script
in the head as follows –
<html>
<head>
<script type = "text/javascript">
function sayHello() {
alert("Hello World")
}
</script>
</head>
<body>
<input type = "button" onclick = "sayHello()" value = "Say Hello" />
</body>
</html>
JavaScript in <body>...</body> section
If you need a script to run as the page loads so that the script generates content in the page, then the script goes in
the <body> portion of the document. In this case, youwould not have any function defined using JavaScript. Take a
look at the following code.
<html>
5
<head>
</head>
<body>
<script type = "text/javascript">
[Link]("Hello World")
</script>
<p>This is web page body </p>
</body>
</html>
JavaScript in <body> and <head> Sections
You can put your JavaScript code in <head> and <body> sections altogether as follows –
<html>
<head>
<script type = "text/javascript">
function sayHello() {
alert("Hello World")
}
</script>
</head>
<body>
<script type = "text/javascript">
[Link]("Hello World")
</script>
<input type = "button" onclick = "sayHello()" value = "Say Hello" />
</body>
</html>
JavaScript in External File
As you begin to work more extensively with JavaScript, you will likely find cases where you are reusing identical
JavaScript code on multiple pages of a site.
You are not restricted to be maintaining identical code in multiple HTML files. The script tag provides a mechanism
to allow you to store JavaScript in an external file and then include it in your HTML files.
To use JavaScript from an external file source, you need to write all your JavaScript source code in a simple text file
with the extension ".js" and then include that file as shown below.
For example, you can keep the following content in the [Link] file, and then you can use the sayHello function in
your HTML file after including the [Link] file.
[Link]
function sayHello() {
alert("Hello World")
Here is an example to show how you can include an external JavaScript file in your HTML code using the script tag
and its src attribute.
You may include the external script reference within the <head> or <body> tag.
6
<html>
<head>
<script type = "text/javascript" src = "[Link]" ></script>
</head>
<body>
...
</body>
</html>
Your First JavaScript Code
Let us take a sample example to print out "Hello World". We call [Link] method which writes a string into
our HTML document. This method can be used to write text, HTML, or both. Take a look at the following code –
<html>
<head>
<title> Your first JavaScript program </title>
<head>
<body>
<script language = "javascript" type = "text/javascript">
[Link]("Hello World!")
</script>
</body>
</html>
JavaScript Values
In JavaScript, you can have two types of values.
Fixed values (Literals)
Variables (Dynamic values)
JavaScript Literals
In the below code, 10 is a Number literal and Hello is a string literal.
JavaScript Values
In JavaScript, you can have two types of values.
Fixed values (Literals)
Variables (Dynamic values)
JavaScript Literals
In the below code, 10 is a Number literal and Hello is a string literal.
<html>
<body>
<script>
[Link](10); // Number Literal
7
[Link]("<br />"); // To add line-break
[Link]("Hello"); // String Literal
</script>
</body>
</html>
This code will produce the following result −
10
Hello
JavaScript Variables
In JavaScript, variables are used to store the dynamic data.
You can use the below keyword to define variables in JavaScript.
var
let
const
You can use the assignment operator (equal sign) to assign values to the variable.
In the below code, variable a contains the numeric value, and variable b contains the text (string).
<html>
<body>
<script>
let a = 5; // Variable Declaration
[Link](a); // Using variable
[Link]("<br>");
let b = "One";
[Link](b);
</script>
</body>
</html>
JavaScript Keywords
JavaScript contains multiple keywords which we can use for a particular task. For example, the function keyword can
be used to define the function. The let, var, and const keywords can be used to define variables.
Lets understand the use of the keyword via the example below.
Example
In this example, we used the function keyword to define the function. We used the var keyword inside the function
to define the sum variable.
Also, we used the let and const keywords outside the function to define two variables and initialize them with
values. After that, we called the function using the function name and passed variables as an argument.
<html>
<body>
<script>
8
function getSum(first, second) {
var sum = first * second;
[Link]("The product of " + first + " and " + second + " is " + sum);
}
let first = 3;
const second = 4;
getSum(first, second);
</script>
</body>
</html>
Comments in JavaScript
JavaScript supports both C-style and C++-style comments, Thus −
Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript.
Any text between the characters /* and */ is treated as a comment. This may span multiple lines.
JavaScript also recognizes the HTML comment opening sequence <!--. JavaScript treats this as a single-line comment,
just as it does the // comment.
The HTML comment closing sequence --> is not recognized by JavaScript so it should be written as //-->.
Example
The following example shows how to use comments in JavaScript.
<script>
// This is a comment. It is similar to comments in C++
/*
* This is a multi-line comment in JavaScript
* It is very similar to comments in C Programming
*/
</script>
Operators in JavaScript
JavaScript contains various arithmetic, logical, bitwise, etc. operators. We can use any operator in JavaScript, as
shown in the example below.
Example
In this example, we have defined var1 and va2 and initialized them with number values. After that, we use the *
operator to get the multiplication result of var1 and var2.
<html>
<body>
<script>
var1 = 10
var2 = 20
9
var3 = var1 * var2;
var4 = 10 + 20;
[Link](var3, " " ,var4);
</script>
</body>
</html>
This code will produce the following result −
200 30
In this way, programmers can use other operators with operands.
Example
In the below code, we have used the assignment and arithmetic expressions.
<html>
<body>
<script>
let a = 10;
let b = 2;
let c = a; // Assigning a value of a to c. Assignment expression.
let d = a + b; // Adding a and b. Arithmetic expression.
let e = a - b; // Subtracting b from a.
[Link]("c = " + c + "<br>");
[Link]("d = " + d + "<br>");
[Link]("e = " + e + "<br>");
</script>
</body>
</html>
Write "Hello World" Program in JavaScript
"Hello, World!" is often the first program, programmers write when learning a new programming language.
JavaScript "Hello World" is a simple program, generally used to demonstrate the basic syntax of the language. This
program will make use of different JavaScript methods to print "Hello World".
Using [Link]()
In JavaScript, the simplest way to print "Hello World" is to use [Link]() method. The [Link]()
method writes the content (a string of text) directly to the HTML document or web page. Lets look at the following −
<html>
<head>
<script>
[Link]("Hello World");
</script>
</head>
<body>
</body>
<html>
Using alert() method
We can use the window alert() method to print "Hello Word" in a dialogue box.
The alert() is a window method that instruct the browser to display an alert box with the message. We can write
alert without passing any message to it.
10
<html>
<head>
<script>
alert("Hello World");
</script>
</head>
<body>
</body>
<html>
JavaScript Comments
JavaScript comments are used to explain the purpose of the code. The comments are not executed as a part of
program and these are solely meant for human developers to understand the code better.
You can use comments for various purposes, such as −
Explaining the purpose of a particular piece of code.
Adding documentation to your code for yourself and others who may read it.
Temporarily disabling or "commenting out" a block of code for testing or debugging without deleting it.
Types of Comments in JavaScript
There are two common ways to write comments in JavaScript −
Single-line comments
Multi-line comments
Single-line comments in JavaScript
We can start the single-line comment with the double forward slash (//). We can write comment between // and
end of the line.
Syntax
The syntax for the single-line comments is given below −
<script>
// single-line comment message
</script>
Example
In the example below, we have explained each step of JavaScript code using single-line comments. In the output, the
code prints the concatenated string.
<html>
<head>
<title> Single line comments </title>
</head>
<body>
<script>
// Defining the string1
var string1 = "JavaScript";
// Defining the string2
var string2 = "TypeScript";
11
// Printing the strings
[Link](string1, " ", string2);
</script>
</body>
</html>
Multi-line comments in JavaScript
The multi-line comment is useful when we require to comment the multiple lines of code or explain the larger codes.
We can write multi-line comments between the /* and */.
Syntax
The syntax for the multi-line comments is given below –
<html>
<head>
<title> Multi line comments </title>
</head>
<body>
<script>
let result = addition(1123, 8823);
[Link]("Result = " + result);
/* This function accepts two values as parameters,
adds them and returns the result*/
function addition(a, b){
var a = 100;
var b = 200;
return a+b;
}
</script>
</body>
</html>
JavaScript - if...else Statement
<html>
<body>
<div id ='output'> </div>
<script type = "text/javascript">
let result;
let age = 20;
if( age > 18 ) {
result = "Qualifies for driving";
}
[Link]("output").innerHTML = result;
</script>
<p> Set the variable to a different value and then try... </p>
</body>
</html>
<html>
<body>
<div id ="demo"></div>
<script type="text/javascript">
const output = [Link]("demo")
12
let book = "maths";
if (book == "history") {
[Link]="<b>History Book</b>";
} else if (book == "maths") {
[Link]="<b>Maths Book</b>";
} else if (book == "economics") {
[Link]="<b>Economics Book</b>";
} else {
[Link]="<b>Unknown Book</b>";
}
</script>
<p> Set the variable to a different value and then try... </p>
</body>
<html>
JavaScript - While Loops
A while statement in JavaScript creates a loop that executes a block of code repeatedly, as long as the specified
condition is true. The condition is evaluated before the execution of the block of code.
While writing a program, you may encounter a situation where you need to perform an action over and over again.
In such situations, you would need to write loop statements to reduce the number of lines.
JavaScript supports all the necessary loops to ease the pressure of programming. In this chapter, we will discuss the
while loop.
There are 2 kinds of while loops in JavaScript, as given below.
Entry-controlled loops − The loop checks whether the looping condition is valid first and enters into the body of the
loop to execute the loop statements.
Exit-controlled loops − The loop enters into the body and executes the loop statements without checking the
condition. After completing the iteration, it checks the condition.
<html>
<body>
<div id = 'output'></div>
<script type="text/javascript">
let output = [Link]("output");
var count = 0;
[Link]="Starting Loop <br>";
while (count < 10) {
[Link]+="Current Count : " + count + "<br>";
count++;
}
[Link]+="Loop stopped!";
</script>
<p> Set the variable to a different value and then try... </p>
</body>
</html>
JavaScript do...while Loop
The do...while loop is similar to the while loop except that the condition check happens at the end of the loop. This
means that the loop will always be executed at least once, even if the condition is false.
13
<html>
<body>
<div id="output"></div>
<script type="text/javascript">
let output = [Link]("output");
var count = 0;
[Link] += "Starting Loop" + "<br />";
do {
[Link] += "Current Count : " + count + "<br />";
count++;
}
while (count < 5);
[Link] += "Loop stopped!";
</script>
<p>Set the variable to a different value and then try...</p>
</body>
</html>
JavaScript while vs. for Loops
The JavaScript while loop is similar to the for loop with the first and third expressions omitted. A for loop is generally
used when the number of iteration is fixed and known but we use the while loop whne the number of iterations is
not known.
Example
Let's take an example of printing the first five natural numbers using for loop –
<html>
<body>
<p> First five natural numbers:</p>
<div id = "demo"> </div>
<script>
const output = [Link]("demo");
for(let i = 1; i <= 5; i++){
[Link] += i + "<br>";
}
</script>
</body>
</html>
<html>
<body>
<p> First five natural numbers:</p>
<div id = "demo"> </div>
<script>
const output = [Link]("demo");
let i = 1;
for(; i <= 5; ){
[Link] += i + "<br>";
i++
}
</script>
</body>
14
</html>
JavaScript - For Loop
The JavaScript for loop is used to execute a block of code repeteatedly, until a specified condition evaluates to false.
It can be used for iteration if the number of iteration is fixed and known.
The JavaScript loops are used to execute the particular block of code repeatedly. The 'for' loop is the most compact
form of looping. It includes the following three important parts
Initialization − The loop initialization expression is where we initialize our counter to a starting value. The
initialization statement is executed before the loop begins.
Condition − The condition expression which will test if a given condition is true or not. If the condition is true, then
the code given inside the loop will be executed. Otherwise, the control will come out of the loop.
Iteration − The iteration expression is where you can increase or decrease your counter.
You can put all the three parts in a single line separated by semicolons.
Examples
Try the following examples to learn how a for loop works in JavaScript.
Example: Executing a code block repeatedly
In the example below, we used the for loop to print the output's updated value of the 'count' variable. In each
iteration of the loop, we increment the value of 'count' by 1 and print in the output.
<html>
<head>
<title> JavaScript - for loop </title>
</head>
<body>
<p id = "output"> </p>
<script>
const output = [Link]("output");
[Link] = "Starting Loop <br>";
let count;
for (let count = 0; count < 10; count++) {
[Link] += "Current Count : " + count + "<br/>";
}
[Link] += "Loop stopped!";
</script>
</body>
</html>
<html>
<head>
<title> Iteration statement is optional </title>
</head>
<body>
<p id = "output"> </p>
<script>
let output = [Link]("output");
let str = "Tutorialspoint";
var p = 0;
15
for (; ;) {
if (p >= [Link]) {
break;
}
[Link] += "str[" + p + "] -> " + str[p] + "<br/>";
p++;
}
</script>
</body>
</html>
JavaScript - For...in Loop
The for...in loop in JavaScript is used to loop through an object's properties. The JavaScript for...in loop is a variant of
the for loop. The for loop can't be used to traverse through the object properties. So, the for...in loop is introduced
to traverse through all object properties.
As we have not discussed Objects yet, you may not feel comfortable with this loop. But once you understand how
objects behave in JavaScript, you will find this loop very useful.
The for...in loop in JavaScript can also be used to iterate through the elements of an array. However, it is not
recommended to do this as this is less efficient than using a for...of loop.
<html>
<head>
<title> JavaScript - for...in loop </title>
</head>
<body>
<p id = "output"> </p>
<script>
let output = [Link]("output");
let car = {
brand: "OD",
model: "Q7",
color: "Black",
}
for (key in car) {
[Link] += key + " -> " + car[key] + "<br>";
}
</script>
</body>
</html>
JavaScript Loop Control
JavaScript provides full control to handle loops and switch statements. There may be a situation when you need to
come out of a loop without reaching its bottom. There may also be a situation when you want to skip a part of your
code block and start the next iteration of the loop.
To handle all such situations, JavaScript provides break and continue statements. These statements are used to
immediately come out of any loop or to start the next iteration of any loop respectively. Also, JavaScript allows
developers to use labels to name the loop.
We have explained the keywords in the below table, which can be used to control the loop.
16
Keyword Explanation
break The 'break' keyword is used to come out of the loop.
continue The 'continue' keyword is used to skip the current iteration of the loop.
label The 'label' is not a keyword, but you can use any identifier followed by a colon (:) to give a label to the loop.
After that, you can use the label to target the particular loop with the break and continue keyword.
The break Statement
The JavaScript break statement, which was briefly introduced with the switch statement, is used to exit a loop early,
breaking out of the enclosing curly braces.
<html>
<body>
<div id = "output"> </div>
<script>
const coutput = [Link]("output");
let x = 1;
[Link] = "Entering the loop<br> ";
while (x < 20) {
if (x == 5) {
break; // breaks out of loop completely
}
x = x + 1;
[Link] += x + "<br>";
}
[Link] += "Exiting the loop!<br> ";
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
The continue Statement
The JavaScript continue statement tells the interpreter to immediately start the next iteration of the loop and skip
the remaining code block. When a continue statement is encountered, the program flow moves to the loop check
expression immediately and if the condition remains true, then it starts the next iteration, otherwise the control
comes out of the loop.
Example
This example illustrates the use of a continue statement with a while loop. Notice how the continue statement is
used to skip printing when the index held in variable x reaches 5 –
JavaScript - Switch Case
The JavaScript switch case is a conditional statement is used to execute different blocks of code depending on the
value of an expression. The expression is evaluated, and if it matches the value of one of the case labels, the code
block associated with that case is executed. If none of the case labels match the value of the expression, the code
block associated with the default label is executed.
17
You can use multiple if...elseif statements, as in the previous chapter, to perform a multiway branch. However, this is
not always the best solution, especially when all of the branches depend on the value of a single variable.
Starting with JavaScript 1.2, you can use a switch statement which handles exactly this situation, and it does so more
efficiently than repeated if...else if statements.
<html>
<head>
<title> JavaScript - Switch case statement </title>
</head>
<body>
<p id = "output"> </p>
<script>
const output = [Link]("output");
let grade = 'A';
[Link] += "Entering switch block <br />";
switch (grade) {
case 'A': [Link] += "Good job <br />";
break;
case 'B': [Link] += "Passed <br />";
break;
case 'C': [Link] += "Failed <br />";
break;
default: [Link] += "Unknown grade <br />";
}
[Link] += "Exiting switch block";
</script>
</body>
</html>
18