# Ternary Statements

The Ternary statement has three parts - the condition, and then the truthy or falsy response.

(condition) ? truthy response : falsy response;
1

The condition can be any expression. It can be as simple as a variable or it can be the result of some calculation. After the condition expression there is a question mark. The rest of the statement is the response. There will be a truthy response that is returned if the condition expression was truthy. This is followed by a colon and then the response if the condition was falsy.

Typically there is a variable on the left side of the ternary statment and the result of the statement is being assigned to that variable.

let info;
let msg = info ? "hello" : "goodbye";
//the string 'goodbye' will be assigned to the variable msg
//because info is undefined, which is a falsy value
1
2
3
4

MDN ternary reference

Back to Week 2 main page

Last Updated: 6/13/2020, 11:30:19 PM