Conditional or ternary Operator
A conditional operator assigns a specified value to a variable based on some conditions, i.e it assigns a value to a variable if a condition is true , otherwise assigns an alternate value if condition is false.
Syntax
variable name = (condition) ? value1 : value2;
Practical Example
Let write a script to check how experience a web developer is, depending on their years of experience in the industry
Javascript code
function checkExp() { var input, output; input = document.getElementById("expcheck").value; output = (input < 2) ? "You're a young developer" : "You're an experienced developer"; document.getElementById("output").innerHTML = output; }
Below is the snippet in codepen
This is actually my first post. Thanks for reading!
Free to hit me up in the comment section.
![[deleted user] image](https://res.cloudinary.com/practicaldev/image/fetch/s--xtH13c6f--/c_limit,f_auto,fl_progressive,q_auto,w_800/https://res.cloudinary.com/practicaldev/image/fetch/s--diPD5F8K--/c_fill,f_auto,fl_progressive,h_150,q_auto,w_150/https://thepracticaldev.s3.amazonaws.com/i/99mvlsfu5tfj9m7ku25d.png)
Top comments (0)