Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check undefined variable in a ternary operator?

I have an issue with ternary operation:

let a = undefined ? "Defined!" : "Definitely Undefined",
    b = abc ? "Defined!" : "Definitely Undefined", // ReferenceError
    c = (abc !== undefined) ? "Defined!" : "Definitely Undefined", // ReferenceError
    d = (typeof abc !== "undefined") ? "Defined!" : "Definitely Undefined"

// results: a = d = "Definitely Undefined", 
// while b and c throw ReferenceError when abc is undefined

What is the best and short way to check if abc is undefined before accessing its properties as well as assign blank object {} if undefined?

let a = [[best way to check abc]] ? {[abc.label1]: 2, [abc.label2]: 1} : {}

PS: I am currently using (typeof abc !== "undefined") in place of [[best way to check abc]]

like image 846
Kiran Shakya Avatar asked Feb 07 '18 08:02

Kiran Shakya


People also ask

How to use the ternary operator to fail in JavaScript?

Enter your marks: 35 You fail the exam. Suppose the use enters 35. Then the condition marks >= 40 evaluates to false. So the second expression fail is assigned to the result variable. In JavaScript, a ternary operator can be used to replace certain types of if..else statements.

What is the meaning of ternary in JavaScript?

The meaning of ternary is composed of three parts. The ternary operator (? :) consists of three operands. It is used to evaluate Boolean expressions. The operator decides which value will be assigned to the variable. It is the only conditional operator that accepts three operands. It can be used instead of the if-else statement.

How to assign a ternary operator to a variable in C?

In C programming, we can also assign the expression of the ternary operator to a variable. For example, Here, if the test condition is true, expression1 will be assigned to the variable. Otherwise, expression2 will be assigned. In the above example, the test condition (operator == '+') will always be true.

How to check if a variable is not null or undefined?

A null value represents the intentional absence of any object value. There are numerous ways to check if a variable is not null or undefined. We are going to use one of the easiest solutions which involve the usage of the typeof and ternary (?) operators.


2 Answers

while b and c throw ReferenceError when abc is undefined

So abc isn't just undefined, it's undeclared. There's a big difference there.

If you need to handle abc being undeclared, the only safe way to do that (without try/catch) is with typeof:

typeof abc === "undefined"

That will be true, without error, if abc is an undeclared identifier. It will also be true if abc is declared and contains the value undefined.

What is the best and short way to check if abc is undefined before accessing its properties as well as assign blank object {} if undefined?

Probably using var to ensure it's declared:

var abc = abc || {};

Duplicate var declarations are not errors (duplicate let declarations are). So with the above, if abc is undeclared, it gets declared with the initial value undefined and we assign it {}. If it's declared, we replace its value with {} if it's falsy. But, if it may or may not be declared with let or const, then the above will throw an error as well.

So to handle the case where it may or may not be declared with let or const, we need a different variable entirely:

let ourabc = typeof abc === "undefined" || !abc ? {} : abc;

That sets ourabc to {} if abc is undeclared or if it contains a falsy value. Since all non-null object references are truthy, and you've said you want to access object properties, that's probably the shortest way.

like image 160
T.J. Crowder Avatar answered Oct 19 '22 16:10

T.J. Crowder


   state = (typeof state !== "undefined") ? state : '';

This way you can check undefined variable in a ternary operator.

like image 36
mujuonly Avatar answered Oct 19 '22 16:10

mujuonly