Conditional statements in javascript
Name | Description |
---|---|
if statement | executing code based on a condition. |
else statement | Alternative code execution when ‘if’ condition is not met. |
else if statement | Additional condition check after ‘if’ and before ‘else’ statement. |
switch statement | Multi-case conditional statement for executing code based on variable’s value. |
Syntax:
if (condition) {
// statement
} else if (condition) {
// statement
} else {
// statement
}
Example: Here is the basic example of using if statement and an else statement and an else if statement.
const num = 0;
if (num > 0) {
console.log("Given number is positive.");
} else if (num < 0) {
console.log("Given number is negative.");
} else {
console.log("Given number is zero.");
};
Output
Given number is zero.
JavaScript Basics
JavaScript is a versatile, lightweight scripting language widely used in web development. It can be utilized for both client-side and server-side development, making it essential for modern web applications. Known as the scripting language for web pages, JavaScript supports variables, data types, operators, conditional statements, loops, functions, arrays, and objects. With JavaScript, you can create dynamic, interactive, and engaging websites.
These are the basics that you need to learn before deep dive into JavaScript:
Table of Content
- Variables in JavaScript
- Data type in JavaScript
- Conditional statements in javascript
- Function in Javascript
- Looping in javascript
- Comments in javascript
- Operators in javascript
Contact Us