ternary operator in js

Solutions on MaxInterview for ternary operator in js by the best coders in the world

showing results for - "ternary operator in js"
Ivy
14 Sep 2018
1condition ? doThisIfTrue : doThisIfFalse
2
31 > 2 ? console.log(true) : console.log(false)
4// returns false
Helen
09 Feb 2018
1//ternary operator syntax and usage:
2condition ? doThisIfTrue : doThisIfFalse
3
4//Simple example:
5let num1 = 1;
6let num2 = 2;
7num1 < num2 ? console.log("True") : console.log("False");
8// => "True"
9
10//Reverse it with greater than ( > ):
11num1 > num2 ? console.log("True") : console.log("False");
12// => "False"
Mariana
17 Apr 2018
1// ternary operators are frequently used as a shorter cleaner if statement
2// condition ? exprIfTrue : exprIfFalse
3
4let age = 15;
5let canDrive = age >= 16 ? 'yes' : 'no';
6// canDrive will be 'no'
7// the condition will be age > 16 which is false so canDrive will equal exprIfFalse
8
9// this ternary is the same as this if else statement
10let age = 15;
11let canDrive;
12if (age >= 16) {
13    canDrive = 'yes';
14} else {
15    canDrive = 'no';
16}
17
18// canDrive will be 'no' because 15 is less than 16
19
20
queries leading to this page
if else shorthand jshow to shorthand if statement in jsjs ternary operatorshort if in javascriptternary statement javascriptjavascript if or ternaryjs ternaryjavascript ternary operatoruse the conditional ternary operator javascripta javascript ternary operatorshorthand if statement javascriptnull in ternary operator jsternary jsif shorthandternary in jsshorthand if jsternary operator in typescriptcall function in ternary operator jsjs short ifternary in findjavascript javascript condition shorthandternary condition javascriptshorthand if javascriptternary operator statement in javascriptjavascript if statement shorthandterninary operatior in javascriptjavascript ternaryternary operator jsternary operator in javascriptelse if block ternary operator jsternary if else if javascriptternary operator javascriptternary operator js examternary op in jsjs shorthand if exists thenjs shorthand ifjavascript short ifternary javascriptternary syntax javascriptternary operator inside ternary operator jshow to use ternary operator in javascriptif statement in assignment javascriptnodejs ternaryif short jsterninary operations javascriptjavascript if shorthandternary operator in jsshort way to do if javascriptternaryelse if ternary jsjavascript tennary operatorif else javascript shortif javascript shorthandtrnary javascriptif condense 3f syntax javascriptshort if javascriptif shorthand javascriptif statement javascript shorthandcall to function inside ternary operator javascriptternary operator es6short if jsterniary operator javascriptjavascript terenaryjavascript ternary statementjs if shorthandternary statement in jswrite if else statement as ternary operator in javascriptternary with null javascriptif condition short form javascriptjavascript shorthand ifshort ifif else shorthand javascriptternary operator javascript exampleif else javascript shorthandjs terciary operatorturnary js operatorjavascript short if statementif else shorten in jsjavascript if else shorthandternary operator in js