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

200
Visualizações
Why string is a primitive in JavaScript?

As far as I know objects can be of any size so you never know how big an object going to be. So it was implemented as a reference type. Unlike any primitive type which size you know in advance.That's what this article says. But strings... from another article:

String : A set of “elements” of 16-bit unsigned integer values. Because string is not a fixed value, the variable assigned a string does not contain the string but the reference to it.

But how is string of a primitive type then?

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

0

That book you're reading is weird.

A primitive type has a fixed size in memory. For example, a number occupies eight bytes of memory, and a boolean value can be represented with only one bit. The number type is the largest of the primitive types. If each JavaScript variable reserves eight bytes of memory, the variable can directly hold any primitive value.

Reference types are another matter, however. Objects, for example, can be of any length -- they do not have a fixed size. The same is true of arrays: an array can have any number of elements. Similarly, a function can contain any amount of JavaScript code. Since these types do not have a fixed size, their values cannot be stored directly in the eight bytes of memory associated with each variable. Instead, the variable stores a reference to the value. Typically, this reference is some form of pointer or memory address. It is not the data value itself, but it tells the variable where to look to find the value.

And then the author goes on to explain how this causes different behaviour, as if that was an effect of the memory layout. But they've got that the wrong way round. The distinction should be:

  • a primitive type has plain, immutable values, that will be copied by value when being passed around.
  • a reference type - synonymous with "the object type" - has mutable objects as values, which have an identity (created when the object is instantiated) and can have arbitrary changeable properties. A variable never holds "an object" itself, but rather a reference to it, and it is this reference that is getting copied when you pass it around.

That (and "arrays and functions are objects") is the gist of the JavaScript value model. How this is implemented doesn't really matter - in fact it differs from engine to engine.

Now when you ask "Why string is a primitive?", the answer is "because strings are immutable values and don't have properties".

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