Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda