I found this snippet of code in my travels in researching JSON:
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
I'm seeing more and more of the ?
and :
notation. I don't even know what it is called to look it up! Can anyone point me to a good resource for this? (btw, I know what !=
means).
JavaScript provides two notations for accessing object properties. The first, and most common, is known as dot notation. Under dot notation, a property is accessed by giving the host object's name, followed by a period (or dot), followed by the property name.
The unsigned right shift operator ( >>> ) (zero-fill right shift) evaluates the left-hand operand as an unsigned number, and shifts the binary representation of that number by the number of bits, modulo 32, specified by the right-hand operand.
The dot (.) is simply an operator that sits between its operands, just like + and -. By convention, the variables stored in an object that we access via the dot operator are generically called properties. Properties that happen to be functions are called methods.
Dot notation is one way to access a property of an object. To use dot notation, write the name of the object, followed by a dot (.), followed by the name of the property. Example: var cat = { name: 'Moo', age: 5, }; console.
It's the ternary conditional operator -- basically,
if (condition) {
a = 4;
}
else {
a = 5;
}
becomes
a = condition ? 4 : 5;
It's called a Conditional (ternary) Operator. It's essentially a condensed if-else.
So this:
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
...is the same as this:
var array;
if (typeof objArray != 'object') {
array = JSON.parse(objArray);
} else {
array = objArray;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With