JavaScript Basic notes 2.
Q.1) Ternary Operator In js.
Ans. It's basically like an if else condition. But there was a major difference between the if else condition and the ternary operator. The ternary operator syntax is too small.
syntax:
condition? true statement: false statement
Let's see with an example👇
Q.2) Switch Statement
Ans. This statement also works as an if-else condition. But there was a difference between syntax. This statement checks the given condition and gives a specific result.
Syntax:
Switch(expression){
case condition1:statement(s)
break;
case condition2:statement(s)
break;
........
........
default : statement(s)
}
Let's see with an example👇
In the upper example, we defined a variable name {color} and we assigned a "red" value to it. Now, In the switch case, we put a variable name on the switch(color)
and in this case, we check a condition if the value stored in a variable name is blue then print the console Ist. and if the condition is false then check on the next line if the condition is true the switch statement prints the value of 2nd console.
Q.3) Alert Box in JS.
Ans. If we want to show an alert message on the website then we can use the alert method.
Syntax:
alert("Message to print");
Let's see an example👇
Q.4) Confirm box in JS
Ans. In this box, we can ask a question to the user. The answer from the user is either true or false.
Syntex:
Confirm("Ask any question");
Let's see an example👇
In the upper alert, the ok is denoted to true and cancel is denoted to false.
Q.5) Prompt Box in js.
Ans. This prompt box is used to get the value from the user which he/she/others entered into the input box.
Syntex:
Prompt("Question");
Let's see an example👇