How to use ternary operator in javascript?
by Gabi SolomonThe ternary operator will accept three operands and is used to assign a certain value to a variable based on a condition. The syntax is condition ? result1 : result2;
So if the the condition is evalued as true the result1 is runned else result2.
Example:
JavaScript:
-
<script language=javascript>
-
var x=3;
-
(x == 3) ? y="true" : y="false";
-
document.write(y);
-
</script>
This will print out: true
Pretty simple, cheers.
Related Posts
-
James
-
j87
-
OriginalCopy
-
Salz`

