0

I am checking for null like this:

let's say c is null.

if (a == null || b == null || c == null || d == null) { //short cirtcuit on the first null value (c)

    let grabNullKey = a == null || b == null || c == null || d == null;

    // I want this to grab the variable that is null, instead this logs `true`

    console.log(grabNullKey)

I want to log the variable name (c) to the user, is there a shorthand to output the variable name instead of doing 4 if statements?

lion_bash
  • 1,309
  • 3
  • 15
  • 27
  • 2
    Why are you trying to log the variable's name? – adiga Jul 03 '19 at 18:40
  • @adiga yes output `c` variable name, updated in question – lion_bash Jul 03 '19 at 18:40
  • But, why do you need this? I mean, it's possible to do this using a chain of if-else or using short hand property names. – adiga Jul 03 '19 at 18:44
  • @adiga Yeah, so I know I can do a big block of if-else statement (or switch statement) to `console.log` the variable name. But if the condition to check is very long it will require a very big block of if-else's, and since we are already short-circuiting on the variable that is null, I was hoping there is a quick way to output the variable name to the logs. It will be useful to log this info so when the condition breaks it's a lot quicker to debug. – lion_bash Jul 03 '19 at 18:48
  • It sounds like you want an array or an object with properties, rather than variables. – VLAZ Jul 03 '19 at 18:48
  • @honeybadger_execute if you resolve an identifier to a value, JS doesn't keep a reference to what the identifier was. If you do `something = foo || bar` you'd get the first non-falsey *contents* of a variable but cannot then reverse it to see where it came from. Unless you write an if-else chain or otherwise manually link the two. – VLAZ Jul 03 '19 at 18:51
  • 2
    Something like `const obj = { a, b, c, d }; const key = Object.keys(obj).find(k => obj[k] === null)` should work. – adiga Jul 03 '19 at 18:53
  • what do you want to do later with the variable name? – Nina Scholz Jul 03 '19 at 18:57
  • @NinaScholz log it, so it'll be easier to debug when an error occured later. Easier for other developers to troubleshoot – lion_bash Jul 03 '19 at 18:59
  • @adiga Will the `.find()` go through the variableObject sequentially? So in the order of (a -> b -> c -> d) – lion_bash Jul 03 '19 at 19:10
  • @honeybadger_execute yes (Guaranteed only in ES2015. You are already using `let`. So, it's fine) – adiga Jul 04 '19 at 06:02

3 Answers3

3

First the bad news, JavaScript doesn't allow you to print a variable name as a string. The good news is there is a way around it.

To be able to print the variable name, you are going to need to use an object instead of a series of variables. So you would need an object like this:

const variableObject = { a: true, b: true, c: null, d: true };

To find the first null value and print it, you would need to loop through they keys and find the first one that is null:

const variableObject = { a: true, b: true, c: null, d: true };
const variableNames = Object.keys(variableObject); // ['a', 'b', 'c', 'd']
const firstNullVar = variableNames.find((key) => variablesObject[key] === null); // 'c'
console.log(firstNullVar); // will print the string 'c'

If none of the variables are null, this will print undefined, although getting around that is easy.

ldtcoop
  • 680
  • 4
  • 14
  • 1
    You can create an object using the existing variables like this: `const variableObject = { a, b, c, d }` [Shorthand property names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015) – adiga Jul 03 '19 at 18:54
  • Will the `.find()` go through the `variableObject` sequentially? (`a` -> `b` -> `c` -> `d`) – lion_bash Jul 03 '19 at 19:08
  • 1
    As long as the key-value pairs are in that order in the object (e.g. `{ a: true, b: true, c: null, d: true }`), they keys do not evaluate to integers, and you are using ES2015, yes. – ldtcoop Jul 03 '19 at 19:14
1

Make an Object Literal and assign the variables as key/value pairs to the object. Pass the object as a parameter to the function demonstrated in the following demo:

function nulls(object)

function nulls(obj) {
  return (Object.keys(obj).filter(key => obj[key] === null)).join(', ');
}

@params object [object]: Object Literal consists of key (variable names, a, b, c,...) and value (variable value , 1, 2, null,...)

  • Object.keys(object) returns an array of keys (variable names)
  • .filter(key => object[key] === null) returns an array of keys (variable names) that have the value (object[key]) of null
  • .join(', ') returns the array of keys (variable names) as a string

function nulls(obj) {
  return (Object.keys(obj).filter(key => obj[key] === null)).join(', ');
}

let x = {
  a: 1,
  b: 2,
  c: null,
  d: 4,
  e: null,
  f: 6,
  g: null
};

console.log(nulls(x));
Community
  • 1
  • 1
zer00ne
  • 41,936
  • 6
  • 41
  • 68
0

think this could do it:

var itemsA = ['a','b','c','d'];
var valsA = [a,b,c,d];
var ind = valsA.indexOf(null);
if(ind != -1){
 console.log(itemsA[ind]); 
}

you can also do something similar by using a JSON object with find or findIndex

Bobert1234
  • 130
  • 12