Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda