0

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?

3
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by Taywin
0

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

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.