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?

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.