JavaScript (JS) is a programming language that is used to create interactive and dynamic web pages. It is a client-side scripting language that runs on the user's web browser. JS Statements are the building blocks of JavaScript programming. They are used to perform specific actions or tasks in a program.
JS Statements are executed one by one in the order they are written. Each statement ends with a semicolon (;) to indicate the end of the statement. The syntax of a JS statement is as follows:
statement;
JS Statements can be categorized into three types:
Variable Declarations are used to declare variables in a program. Variables are used to store data values that can be used later in the program. The syntax of a variable declaration statement is as follows:
var variableName = value;
Here, var
is a keyword that is used to declare a variable. variableName
is the name of the variable, and value
is the value that is assigned to the variable. For example:
var x = 5; var y = "Hello World!";
In the above example, x
is a variable that stores the value 5, and y
is a variable that stores the string "Hello World!".
Expressions are used to perform operations on data values. They can be used to perform arithmetic operations, comparison operations, and logical operations. The syntax of an expression statement is as follows:
expression;
Here, expression
is the expression that is evaluated. For example:
var x = 5; var y = 10; var z = x + y;
In the above example, the expression x + y
is evaluated and the result is stored in the variable z
.
Control Structures are used to control the flow of a program. They can be used to perform conditional statements, loops, and functions. The syntax of a control structure statement is as follows:
controlStructure { // code block }
Here, controlStructure
is the control structure that is used, and code block
is the code that is executed if the condition is true. For example:
if (x > y) { console.log("x is greater than y"); } else { console.log("y is greater than x"); }
In the above example, the if
statement is used to check if x
is greater than y
. If the condition is true, the code block inside the if
statement is executed. If the condition is false, the code block inside the else
statement is executed.
JS Statements are an essential part of JavaScript programming. They are used to perform specific actions or tasks in a program. By understanding the different types of JS Statements, you can create more complex and dynamic web pages.
Variable Declaration:
<p>Click the button to display the value of the variable.</p> <button onclick="myFunction()">Click me</button> <p id="demo"></p> <script> function myFunction() { var x = 5; document.getElementById("demo").innerHTML = x; } </script>
Expression:
<p>Click the button to display the result of the expression.</p> <button onclick="myFunction()">Click me</button> <p id="demo"></p> <script> function myFunction() { var x = 5; var y = 10; var z = x + y; document.getElementById("demo").innerHTML = z; } </script>
Control Structure:
<p>Click the button to check if x is greater than y.</p> <button onclick="myFunction()">Click me</button> <p id="demo"></p> <script> function myFunction() { var x = 5; var y = 10; if (x > y) { document.getElementById("demo").innerHTML = "x is greater than y"; } else { document.getElementById("demo").innerHTML = "y is greater than x"; } } </script>