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

199
Views
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 answers
Answer question

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 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!