Hi there,

I would like to know what the differences between primitive string type and object string type are because I declared both of them and had a look the functions that I can invoke on them. They are the same functions.

Can anyone explain the differences/similarities please?

Dani AI

Generated

A short, practical follow-up to ’s question (and to ’s reference and ’s pointer about Boolean pitfalls).

Primitives (e.g. var s = "hi") are simple, immutable values. Wrapper objects (created with new String("hi")) are full objects. JavaScript transparently boxes primitives when you call methods on them, so s.length and new String("hi").length both work — but the underlying types and behaviors differ in important ways:

var p = "hi";
var o = new String("hi");

console.log(typeof p);    // "string"
console.log(typeof o);    // "object"

console.log(p == o);      // true  (loose equality unboxes)
console.log(p === o);     // false (different types)

p.prop = 1;
o.prop = 1;
console.log(p.prop);      // undefined
console.log(o.prop);      // 1

Notes and cautions

  • Use typeof x === "string" for primitives; x instanceof String is true only for wrapper objects. For a robust check use Object.prototype.toString.call(x).
  • Wrapper objects allocate and behave like objects: you can attach properties to them, and any object is truthy. That means new Boolean(false) is truthy even though it wraps false — a common trap related to ’s remark about Boolean objects and conditionals.
  • Equality: == may coerce/unbox; prefer === to avoid surprises.

Recommendations

  • Prefer literal primitives ("text", true, 42). Convert with String(x), Boolean(x), Number(x) when needed — don’t use new.
  • If you receive mixed input, normalize early (e.g., value = String(value) or value = !!value) and then test with typeof or ===.
  • To force a primitive out of a wrapper, use .valueOf().

These points explain why methods look the same for both forms but why primitives are usually the safer, clearer choice.

Recommended Answers

All 5 Replies

Thanks for the article.

I would like to comment on using Boolean object and the "if" statement. I am not sure that the Boolean object is that useful in JavaScript. The only reason I would use it when I want to make sure that the incoming argument is really a boolean by checking the type of the object. If you expect a primitive boolean coming from a function argument, you could use toString() for checking the value instead. It is good for the article to mention about the class object.

For the "if" statement, there is a pit fall in JavaScript that may not be expected by those who are not familiar with scripting language. As you know, null, undefined, and false will evaluate as "false" and will not execute the statements inside the scope. However, in JavaScript, value "0" or an empty string "" will be evaluated as false as well.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function testIfStatement(v) {
  if(v) { alert("Evaluate undefined: true") }  // undefined
  else { alert("Evaluate undefined: false") }
  v = null
  if(v) { alert("Evaluate null: true") }  // null
  else { alert("Evaluate null: false") }
  v = ""
  if(v) { alert("Evaluate empty string: true") }  // empty string
  else { alert("Evaluate empty string: false") }
  v = 0
  if(v) { alert("Evaluate zero: true") }  // zero value
  else { alert("Evaluate zero: false") }
}
</script>
</head>

<body>
<!-- passing in undefined value variable to the function -->
<input type="button" value="RUN Test" onclick="testIfStatement()">
</body>
</html>

value "0" or an empty string "" will be evaluated as false as well.

That is kinda tricky then. Because the length of "0" is one hopefully and the length of "" is zero I hope you agree on this.

Sorry, it is my writing issue. :P I want to emphasize the 0 by putting double quotation marks around it. Sorry about that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.