最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript shortest possible check for undefined, if it is assign a default value - Stack Overflow

programmeradmin3浏览0评论

If passing params or an object full of properties into function, it's useful to check for undefined params and give default values to those that are undefined.

Without using a library function like jQuery extent, what would be the shortest about of code to do this kind of assigning defaults?

Here is the shortest I can think of:

var test;
var output = (typeof test != "undefined") ? test : "Default";

Before someone suggests:

var test;
var output = test || "Default";

That will not work with false, 0 or ""

If passing params or an object full of properties into function, it's useful to check for undefined params and give default values to those that are undefined.

Without using a library function like jQuery extent, what would be the shortest about of code to do this kind of assigning defaults?

Here is the shortest I can think of:

var test;
var output = (typeof test != "undefined") ? test : "Default";

Before someone suggests:

var test;
var output = test || "Default";

That will not work with false, 0 or ""

Share Improve this question asked Dec 14, 2012 at 17:21 FergalFergal 2,4744 gold badges36 silver badges49 bronze badges 3
  • undefined is a keyword so you shouldn't need to place it in quotes. – War10ck Commented Dec 14, 2012 at 17:32
  • 2 @War10ck typeof operator always returns a string, that's why quotes are needed. – Teemu Commented Dec 14, 2012 at 17:59
  • @Teemu Did not know that. Guess I need to look into that more. Learn something new everyday. My apologies for that. Thanks for the correction. :) – War10ck Commented Dec 14, 2012 at 18:03
Add a ment  | 

5 Answers 5

Reset to default 5

Try this:

var output = test != null ? test : "Default";

I.g:

null == undefined // true
null == null // true
null == 0 // false
null == "" //false
null == false // false

In ES2021 this is now possible using the Nullish coalescing operator.

Usage:

const str = null ?? 'default string';
console.log(str);
// expected output: "default string"

const num = 0 ?? 42;
console.log(num);
// expected output: 0

const bool = false ?? true;
console.log(bool)
// expected output: false

Ok, so if the "undefined" it the longest value of types, the shortest way to check object presence is

!(typeof console).charAt(8)

where "console" is our examinated object. .charAt(8) will return empty string "" if object is anything else than "undefined", that equals false It's a safe way for old IE supporting. But if u need only modern browsers support, you can use just

if(!(typeof console)[8]) { doWork(); }

to check that object is defined.

https://github./jed/140bytes/wiki/Byte-saving-techniques is your friend:

var output = test===[]._ ? "Default" : test;

Shorter:

var output=test===[]._?"Default":test;

No spaces needed between operators and tokens.

You could try this:

var output = if(test === undefined) ? test: "Default";

If you know that you will never pass 0, false, or "" as valid parameters you could simplify this to be:

var output = if(test) ? test : "Default";

However, this gets into the cryptic "truthy falsey" part of JavaScript. By having the === you are doing an equality check with type parison.

Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论