3

I have ~20 variables that I want to check whether they are empty or not. Instead of writing "ugly code" in form of individual if (!a) { var a = 'N/A' } statements, I was wondering if there is some form of shorthand similar to PHP?

I want to check if the variable is empty, and if it is, set its value to 'N/A'.

Edit: In reference to what I mean regarding PHP shorthand, I am looking for something like this.

$is_admin = ($user['permissions'] == 'admin') ? true : false;

Following the suggestion to keep variables in an array, please see my code below. I am pulling data via AJAX from my MySQL database and am outputting it as part of the data[] array. How would I go about checking if any of the data variables have value or not?

$.ajax({                                            // launch AJAX connection
    type: 'POST',                                   // via protocol POST
    url: '../../plugins/MySQL/ajax_action.php',
    data: { action: 'opera_lookup' , holidex: '<?php echo($_SESSION['Holidex']); ?>' , conf_no: $("#room").val() },         // send $_POST string
    dataType: 'json',                               // encode with JSON
    success: function (data)
    {       

        var data0 = data[0];
        // 20 more variables here...

    },
});
Armitage2k
  • 1,164
  • 2
  • 27
  • 59
  • What *form of shorthand similar to PHP* are you referring to? – Robby Cornelissen Jan 24 '17 at 03:36
  • Be sure you understand js falsy values: https://developer.mozilla.org/en-US/docs/Glossary/Falsy – Faust Jan 24 '17 at 03:37
  • Instead of having twenty variables, keep their values in an array or an object. Then you can easily iterate across the array or object, and do the check on each value. By the way, what exactly do you mean by "empty"? –  Jan 24 '17 at 03:38
  • 1
    the fact that you have `var` inside a block statement `{}` shows that there is much wrong with your code - and much you need to learn about javascript and it's hoisting of variable declarations – Jaromanda X Jan 24 '17 at 03:39
  • @torazaburo I don't think this question matches the duplicate as this one specifically deals with multiple variables. – Robby Cornelissen Jan 24 '17 at 04:00
  • 1
    Your PHP "shorthand" could just be rewritten as `$is_admin = $user['permissions'] == 'admin';` – Robby Cornelissen Jan 24 '17 at 04:02
  • Just an example :) I am looking for a JS equivalent where I can quickly define the value if the statement is true or false. I am more familiar with PHP than JS, hence the question. – Armitage2k Jan 24 '17 at 04:04

1 Answers1

9

This is idiomatic JavaScript:

a = a || 'N/A';

Note that a will take the value 'N/A' for all falsy values of a.

To handle multiple variables: ECMAScript 6's destructuring assignment lets you assign default values to the variables that are undefined in an array of variables:

var a = "some value";
var b = "";
var c = "some other value";
var d;

[a='N/A', b='N/A', c='N/A', d='N/A'] = [a, b, c, d];

console.log(a); // "some value"
console.log(b); // ""
console.log(c); // "some other value"
console.log(d); // "N/A"

Or you can use the destructuring assignment in combination with map() to treat all falsy values as in my first example:

var a = "some value";
var b = "";
var c = "some other value";
var d;

[a, b, c, d] = [a, b, c, d].map(x => x || 'N/A');

console.log(a); // "some value"
console.log(b); // "N/A"
console.log(c); // "some other value"
console.log(d); // "N/A"

If you don't need to maintain the separate variables, you can of course also just apply map to the data array directly:

var data = ["some value", "", "some other value", undefined];

data = data.map(x => x || 'N/A');

console.log(data); // ["some value", "N/A", "some other value", "N/A"]
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • I like the `map()` solution, looks cleaner. Is there any way for me to loop through the `data[]` and array and check each value without having to assign it a variable first (eg. data0, data1, etc.)? – Armitage2k Jan 24 '17 at 04:05
  • 1
    Of course. `data = data.map(x => x || 'N/A');` – Robby Cornelissen Jan 24 '17 at 04:07