2

I always thought that one of the advantages of using this formulation of console.log will save you from null pointer exceptions and the like:

var c = undefined;
console.log(c,"foo");

now we get 

"undefined 'foo'"

however, there doesn't really seem to be much of an advantage, since using the + operator seems to coerce null's and undefined's into strings before concatenation:

var c = undefined;
console.log(c + "foo");

this simply logs

"undefinedfoo"

Is there any way to get a null pointer or some error thrown from concatenating strings with the + operator or console.log(x,y) in JS? You of course can get a null pointer exception/error doing undefined.concat('foo').

is there any way to get a null point exception when concatenating Strings in JavaScript?

8
  • 1
    javascript has no pointers, therefore getting a null pointer exception would essentially be impossible. Commented Jul 22, 2015 at 19:57
  • ok, Marc then what happens when you call undefined.concat(''); Commented Jul 22, 2015 at 20:00
  • "TypeError: undefined has no properties", as you would expect... Commented Jul 22, 2015 at 20:00
  • yes because undefined does not point to an object that has a concat function property, touché en.wikipedia.org/wiki/Null_pointer Commented Jul 22, 2015 at 20:04
  • despite what the internet says, it's the same soup, don't believe the hype that "JS doesn't have null pointers" Commented Jul 22, 2015 at 20:05

2 Answers 2

1

is there any way to get a null point exception when concatenating Strings in JavaScript?

Technically, no (JavaScript doesn't have Null Pointer Exceptions).

In the spirit of what your'e asking, though, yes. ES6 Symbols will actually throw if you try to implicitly coerce them to strings. Additionally, you can also intentionally screw around with the implicit toString() behavior:

var obj = { toString: function() { throw 'uh oh'; } };
console.log(obj + ''); // throws

For day-to-day development, though, it's pretty safe to assume string coercion will succeed. Maybe this will change in future years as Symbols become more ubiquitous, though.

Sign up to request clarification or add additional context in comments.

Comments

0

Short answer: no.

Long answer: JavaScript doesn't enforce types and handles type conversion a little bit weirdly. It is happy to try to render pretty much anything as a string without complaining.

However, if you want to have JavaScript freak out when a string turns out to be null, you can use assertions. JavaScript doesn't have assertions built-in like many languages do, but this answer can help you to manually add an assert function. Using that, you could do something like this to enforce that a string not be null:

assert(typeof myString !== 'undefined')

2 Comments

"It is happy to try to render pretty much anything as a string without complaining" This is 99.9999999% accurate. Please see my answer for the .0000001%.
@jmar777 I knew there had to be something that couldn't be coerced into a string! Oh JavaScript, you strange strange creature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.