Casting to string in JavaScript

They do behave differently when the value is null.

  • null.toString() throws an error – Cannot call method ‘toString’ of null
  • String(null) returns – “null”
  • null + "" also returns – “null”

Very similar behaviour happens if value is undefined (see jbabey’s answer).

Other than that, there is a negligible performance difference, which, unless you’re using them in huge loops, isn’t worth worrying about.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)