Was it helpful?

Question

Changing ternary operator into non-ternary - JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

For ternary operator alternative, simply use if else in JavaScript. Let’s say, we have two numbers −

var number1=12;
var number2=12;

To compare, we can use if else, instead of the ternary operator −

if(number1==number2)
console.log("true");
else
console.log("false");

Example

Following is the code −

var number1=12;
var number2=12;
var result=(number1==number2)?true:false;
console.log(result);
if(number1==number2)
   console.log("true");
else
   console.log("false");

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo217.js.

Output

The output is as follows −

PS C:\Users\Amit\JavaScript-code> node demo217.js
true
true
raja
Published on 03-Oct-2020 17:26:18
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top