28

I've always converted numbers to strings by adding an empty string to them:

var string = 1 + '';

However, JSLint complains of this method with Expected 'String' and instead saw ''''., and it does look a little ugly.

Is there a better way?

Samuel Cole
  • 2,005
  • 2
  • 14
  • 14

7 Answers7

22

I believe that the JSLint approved way is to call .toString() on the number:

var stringified = 1..toString();
// Note the use of the double .. to ensure the the interpreter knows 
// that we are calling the toString method on a number -- 
// not courting a syntax error.
// You could also theoretically call 1["toString"];
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • 1
    That's the ticket. 1.toString() was failing in the console for that reason. – Samuel Cole Apr 28 '11 at 16:39
  • 9
    `(1).toString()` would do, `String(1)` also, and furthermore the conversion is implicitly done on for example 'duck'+1 (JsLint approved). JsLint only disagrees with addition of an empty string, which somehow makes sense: why would anyone do `var ducked = 'duck'+''`? – KooiInc Apr 28 '11 at 17:16
8

(Sorry, it possibly would've been better to say this as a comment above but I haven't yet earned the right to post comments, so...)

Remember that jslint is not just validating whether your JavaScript will actually run, it is trying to enforce coding style with the aim of helping you produce more readable and maintainable code.

So 1 + '' works, but isn't necessarily the most readable option for everybody while explicit casting options (see the other answers) should be readable for everybody. Of course if nobody else will ever see your code you need only worry about whether you will be able to understand it if you come back to it next month, or next year...

Don't forget that the following two statements don't produce the same result:

var s1 = 1 + 3 + ''; // gives '4'
var s2 = '' + 1 + 3; // gives '13'

I assume 1 + '' is just a simplification for discussion though, or why not just use '1' in the first place?

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • I actually have 3 integers that I want to combine to a string (like in s2) - what would be the best way to do that? – Daniel G. Jun 04 '14 at 13:01
  • @DanielG. `result = '' + 1 + 2 + 3;` will result in the string `'123'`. You can concatenate variables the same way. – nnnnnn Jun 05 '14 at 03:36
  • Sure, that's what I'm doing at the moment - but this is not "Lint-Valid". :) – Daniel G. Jun 11 '14 at 10:14
3

You can use the .toString() method like so:

var num = 1;
var str = num.toString();
KushalP
  • 10,976
  • 6
  • 34
  • 27
3

There's also (at least in Chrome): String(1) without new.

var n = 1, s = String(n);
Rudie
  • 52,220
  • 42
  • 131
  • 173
2

I am going to say "bug" or "mis-feature"

Cole Consider that

var x = "foobar" + 1;

is "approved" jslint. In any case, it is 100% valid Javascript.

Happy coding.


For comment:

This is why I prefer to use the string literal as the first operand as this shows intent -- knowing a language is fundamental to using a language.

The only place the duck-typing is an issues (in this case) is with an expression of the form a + b, where neither is a string literal. In this case a (or b) may evaluate to a string where it was expected to evaluate to a number (this would trigger the string concatenation vs. the expected numeric addition). If any of the operands are a string literal the intent is well-defined/described.

This particular "issue", however, is not present in the posted code; nor would it be eliminated with the use of toString over a string literal.

  • Yeah, but 1 + '' is ugly. It doesn't actually describe what we want, it just works as a coincidence of duck-typing – Samuel Cole Apr 28 '11 at 16:41
  • Sure but as mentioned in another answer, doesn't 1..toString() make it even more clear? – Samuel Cole Apr 28 '11 at 16:46
  • @Samuel Cole Not to me. Why would it? I also like/utilized the fact that I can do `"" + x` and not explode when x is null or undefined. (This isn't always desired, but knowing the rules allows one to make an appropriate decision as far as what construct is acceptable.) –  Apr 28 '11 at 16:48
  • Yeah, I'm absolutely not arguing against the case where you want to build a string including a number: `console.log('I have ' + number_of_ducks + ' ducks!');` I'm arguing against using concatenation simply for casting. Not having an exception thrown is valuable, but I think a try..catch might be in order in that case. – Samuel Cole Apr 28 '11 at 16:55
  • @Samuel Cole I don't consider the operations to be different. The end result is a string representation of the number following a particular rule. In any case, I prefer to write code that is "idempotent" to structural changes and selectively using `toString` does not further this cause. –  Apr 28 '11 at 17:08
1

In my opinion, we should use String(number) instead of number + '' or number.toString(), because

  • number + '' will trigger the JSLint error

  • number.toString() will fail in case number is null or undefined, and you will have TypeError: number is undefined / null

enpith
  • 1,540
  • 2
  • 10
  • 11
0

Recently, JSLint was in beta. The new version no longer complains about this code:

function convert(x) {
    'use strict';
    // alert(typeof x);
    x = x + "";
    // alert(typeof x);
    return x;
}


convert(3);
Karl
  • 1,814
  • 1
  • 25
  • 37