What is The Ternary Operator In JavaScript ?

Here is The syntax:

So basically , It is another way to write the conditional statements (if / else / else if ) in a much simpler way. It is especially used for the non-complex cases.
for example , instead of this:

let a = 4;
if (a > 4)
{
consol…


This content originally appeared on DEV Community and was authored by yns

Here is The syntax:
Ternary operator syntax
So basically , It is another way to write the conditional statements (if / else / else if ) in a much simpler way. It is especially used for the non-complex cases.
for example , instead of this:

let a = 4;
if (a > 4)
{
console.log("ok");
}
else
{
console.log("not ok");
}

We can just do this:

a > 4 ? console.log("ok") : console.log("not ok");


This content originally appeared on DEV Community and was authored by yns


Print Share Comment Cite Upload Translate Updates
APA

yns | Sciencx (2024-08-14T14:51:06+00:00) What is The Ternary Operator In JavaScript ?. Retrieved from https://www.scien.cx/2024/08/14/what-is-the-ternary-operator-in-javascript/

MLA
" » What is The Ternary Operator In JavaScript ?." yns | Sciencx - Wednesday August 14, 2024, https://www.scien.cx/2024/08/14/what-is-the-ternary-operator-in-javascript/
HARVARD
yns | Sciencx Wednesday August 14, 2024 » What is The Ternary Operator In JavaScript ?., viewed ,<https://www.scien.cx/2024/08/14/what-is-the-ternary-operator-in-javascript/>
VANCOUVER
yns | Sciencx - » What is The Ternary Operator In JavaScript ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/14/what-is-the-ternary-operator-in-javascript/
CHICAGO
" » What is The Ternary Operator In JavaScript ?." yns | Sciencx - Accessed . https://www.scien.cx/2024/08/14/what-is-the-ternary-operator-in-javascript/
IEEE
" » What is The Ternary Operator In JavaScript ?." yns | Sciencx [Online]. Available: https://www.scien.cx/2024/08/14/what-is-the-ternary-operator-in-javascript/. [Accessed: ]
rf:citation
» What is The Ternary Operator In JavaScript ? | yns | Sciencx | https://www.scien.cx/2024/08/14/what-is-the-ternary-operator-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.