Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

JavaScript: Conditional Operator

?: (Conditional operator)

The conditional operator is used as a shortcut for standard if statement. It takes three operands.

Syntax

Condition ? expr1: expr2

Parameters

condition : An expression that evaluates to true or false.

expr1, expr2 : Expressions with values of any types.

If the condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.

For example

status = (marks >= 30) ? "Pass" : "Fail"

The statement assigns value "Pass" to the variable status if marks are 30 or more. Otherwise, it assigns the value of "Fail" to status.

Example:

In the following web document the conditional operator statement [status = (marks >= 30) ? "Pass" : "Fail"] assigns value "Pass" to the variable status if marks are 30 or more. Otherwise, it assigns the value of "Fail" to status.

HTML Code

<!doctype html><head>
<meta charset="utf-8">
<title>JavaScript conditional operator example with DOM</title>
<meta name="description" content="This document contains an example of JavaScript conditional operator with DOM" />
</head>
<form name="example" onsubmit="ViewOutput()">
<input type="text" id="marks" placeholder="disabled" />
<input type="submit" value="Submit" />
</form>
<body>
<script src="javascript-conditional-operator-example1.js">
</script>
</body>
</html>

JS Code

function ViewOutput()
{
'use strict';
var marks = document.getElementById("marks").value;
var status1 = (marks >= 30) ? "Pass" : "Fail";
var newParagraph = document.createElement("p"); //creates a new paragraph element
var newText = document.createTextNode(status1); //creates text along with ouput to be displayed 
newParagraph.appendChild(newText); //created text is appended to the paragraph element created
document.body.appendChild(newParagraph); // created paragraph and text along with output is appended to the document body
}

View the example in the browser

JavaScript: Conditional Operator and If else statement

The conditional operator statement of the above example
status = (marks >= 30) ? "Pass" : "Fail" is equivalent to the following statement.

if marks>=30
 document.write("Pass");
 else
 document.write("Fail");
 

See also

comma
delete
function
in
instanceof
new
this
typeof
void

Want to Test your JavaScript skill ?

Want to Practice JavaScript exercises ?

Previous: JavaScript: String Operators
Next: JavaScript: Comma Operator

Test your Programming skills with w3resource's quiz.



JavaScript: Tips of the Day

How to insert an item into an array at a specific index (JavaScript)?

What you want is the splice function on the native array object.

arr.splice(index, 0, item); will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert). In this example we will create an array and add an element to it into index 2:

var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";

console.log(arr.join());
arr.splice(2, 0, "Lene");
console.log(arr.join());

Ref: https://bit.ly/2BXbp04