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

231
Visualizações
Comparison Operator in JavaScript
const abc = 5;
const bcd = 5

console.log(abc===bcd)/// return true

This result is surprising to me.

Since abc will be assigned a different memory location ...and bcd has also a different location

why this is returning true

If the code was

const abc = 5
const bcd = abc

console.log(abc === bcd) ///true here make sense

I seem to be be really confused with the first case since abc and bcd are two different variables and have no relation between them

Any related article or blog would really help.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In javascript all primitives (string, nubmer, bigint, boolean, undefined, symbol, null) are immutable and will be compared by value:

All primitives are immutable, i.e., they cannot be altered. It is important not to confuse a primitive itself with a variable assigned a primitive value. The variable may be reassigned a new value, but the existing value can not be changed in the ways that objects, arrays, and functions can be altered.


In comparison to that objects (this also includes arrays and functions, basically everything that is not a primitive) are mutable and will be compared by identity.

Example:

console.log("Primitives compare by value:");
console.log(5 === 5); // true
console.log("foo" === "foo"); // true
console.log(true === true); // true


console.log("Objects compare by identity:");
console.log({} === {}); // false
console.log([] === []); // false
console.log(function(){} === function(){}); // false


Primitive Wrappers

Javascript also has wrapper objects for the primitive types, which might be the source of your question.
These wrappers wrap a primitive value, and are - as the name suggests - objects. So for primitive wrapper instances your code would be correct:

let a = new Number(1);
let b = new Number(1);

console.log("Primitive wrappers are objects:");
console.log(a === a); // true
console.log(a === b); // false

about 4 years ago · Juan Pablo Isaza Relatório

0

There is a fundamental difference in JavaScript between primitive values (undefined,null, booleans, numbers, and strings) and objects (including arrays and functions)

I. Primitives are compared by value:

  1. Two values are the same only if they have the same value.
  2. If two distinct string values are compared, JavaScript treats them as equal if, and only if, they have the same length and if the character at each index is the same.

II. Objects are different than primitives.

Objects are not compared by value: Two distinct objects are not equal even if they have the same properties and values.

Therefore, Objects are sometimes called reference types to distinguish them from JavaScript’s primitive types.

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