PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] How to check empty in variable in Google Apps Script

 October 28, 2022     google-apps-script, is-empty, javascript     No comments   

Issue

I have a variable that can be either empty, number or text. I like to find only empty one. But the following codes using .length returned null for 0 number, though it returned 1 for "0" string . .toString.length even didn't work. Any suggestion? Thank you!

function test() {
  // criteria can be either empty, number or text. How can I check whether the critieria is empty?
  // In the example below, critiera_2.length returned null, not 1.
  criteria_1 = "";
  Logger.log(criteria_1.length);
  criteria_2 = 0;
  Logger.log(criteria_2.length);
  criteria_3 = "0";
  Logger.log(criteria_3.length);
  criteria_4 = "X";
  Logger.log(criteria_4.length);


  criteria_1 = "";
  Logger.log(criteria_1.toString.length);
  criteria_2 = 0;
  Logger.log(criteria_2.toString.length);
  criteria_3 = "0";
  Logger.log(criteria_3.toString.length);
  criteria_4 = "X";
  Logger.log(criteria_4.toString.length);
}

Solution

criteria_1 = "";
console.log(criteria_1.toString() == ''); // output: true

const test = x => console.log(x.toString()==='');

test("");  // true
test(0);   // false
test("0"); // false
test("X"); // false

It's turned out that you don't even need toString() it could be just x===''



Answered By - Yuri Khristich
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing