Answers for "javascript shorthand ternary"

50

js ternary

condition ? ifTrue : ifFalse
Posted by: Guest on April-10-2020
3

how to use ternary operator in javascript

function example(…) {
    return condition1 ? value1
         : condition2 ? value2
         : condition3 ? value3
         : value4;
}

// Equivalent to:

function example(…) {
    if (condition1) { return value1; }
    else if (condition2) { return value2; }
    else if (condition3) { return value3; }
    else { return value4; }
}
Posted by: Guest on April-28-2020
2

ternary operator shorthand javascript

let startingNum = startingNum ? otherNum : 1
// can be expressed as
let startingNum = otherNum || 1

// Another scenario not covered here is if you want the value 
// to return false when not matched. 
//The JavaScript shorthandfor this is:
let startingNum = startingNum ? otherNum : 0
// But it can be expressed as
let startingNum = startingNum && otherNum
Posted by: Guest on June-13-2020
0

make shorter if statements with objects

var name_map = {
        "#first": "First Name",
        "#middle": "Middle Name",
        "#last": "Last Name"
}

if (Object.keys(check_map).every(selector => $(selector).val())) { 
	  // Yes, they all have non-blank values
} else {
	// No, at least one of them has a blank value (or didn't exist at all)
}
Posted by: Guest on June-15-2020
0

javascript shorthand ternary

var startingNumber = startingNumber || 1;
Posted by: Guest on October-12-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language