Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
How to set default value of a parameter of a function when it is 'undefined' or 'null' or ''(empty string)?

colorX is my parameter from some function.

colorX = typeof colorX !== ('undefined' || 'null' || '') ? colorX : 'abc';

Here for 'undefined' I'm getting abc as value. But when I pass null or 'null' or empty string as parameter input. I'm getting error.

Also what is the difference between null and 'null'?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Explanation

null vs 'null'

When using typeof, it expects a string version of the type, such as 'undefined', 'function', 'bigint', and so on. However, specifically for the null type it functions as an object:

console.log(typeof null); // expected output: object
console.log(typeof 'strExample'); // expected output: string

// We can return a boolean by doing so:

let variable = 10;
console.log(typeof variable === 'string'); // expected output: false

The explanation as to why can be found on MDN:

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the typeof return value "object". (reference)

A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in typeof null === 'null'.

In any other scenario, 'null', 'undefined', and 'string' would be identified as strings since they are surrounded in '''s, where this is what @reyno was referring to.

See all accepting values for typeof here.

Checking for null

// null will be identified as an "object" type; checking
// null with typeof is redundant as {} can be the same as null.

console.log(typeof null); // expected output: object
console.log(typeof {}); // expected output: object

// so we can just do:

let colorX = null;
console.log(colorX === null); // expected output: true

Resulting Code

Since falsy values can be any of these:

Besides false, possible falsy expressions are: null, NaN, 0, the empty string (""), and undefined.

MDN

We can simply do:

colorX = colorX || 'abc';

As @ivar claims.

about 4 years ago · Juan Pablo Isaza Report

0

Using the ternary operator you could simplyfy this to

colorX = colorX ? colorX : 'abc';

The Conditional (ternary) operator syntax is

condition ? exprIfTrue : exprIfFalse

and according to the MDN docs

...null, NaN, 0, the empty string (""), and undefined. If condition is any of these, the result of the conditional expression will be the result of executing the expression exprIfFalse.

But alternatively and even shorter (in case you are checking for all 'falsy' values) you might as well use the logical OR

colorX = colorX || 'abc';

Since you say you want to check for all falsy values except for 0 you can combine both

let v = 0

v = v || v === 0 ? v : 'value'

console.log(v) // 0

Notice that

null, undefined, 0

with quotes simply become strings containing the characters without "functional meaning"

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!