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?

  • 1
    javascript has no pointers, therefore getting a null pointer exception would essentially be impossible. – Marc B Jul 22 '15 at 19:57
  • ok, Marc then what happens when you call undefined.concat(''); –  Jul 22 '15 at 20:00
  • "TypeError: undefined has no properties", as you would expect... – Marc B Jul 22 '15 at 20:00
  • yes because undefined does not point to an object that has a concat function property, touché https://en.wikipedia.org/wiki/Null_pointer –  Jul 22 '15 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" –  Jul 22 '15 at 20:05
  • by definition, undefined is a null pointer. that's what it is. by definition. –  Jul 22 '15 at 20:06
  • wrong. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined it's a defined type: a primitive value. – Marc B Jul 22 '15 at 20:07
  • Marc, undefined is a type, and that type represents a null pointer. that's the rub, they are not mutually exclusive. –  Jul 22 '15 at 20:09

2 Answers2

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.

jmar777
  • 38,796
  • 11
  • 66
  • 64
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')

Community
  • 1
  • 1
Hayden Schiff
  • 3,280
  • 19
  • 41
  • "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 Jul 22 '15 at 20:02
  • 1
    @jmar777 I knew there had to be something that couldn't be coerced into a string! Oh JavaScript, you strange strange creature. – Hayden Schiff Jul 22 '15 at 20:06