Nikolay
Nikolay

Reputation: 89

JavaScript calculating length of a string

Why when I try this block of code:

var str = "1234";
alert(str.toString().lenght);

it alerts me 'undefined' ?

Upvotes: 1

Views: 1151

Answers (4)

mamoo
mamoo

Reputation: 8166

Just a typo: it's .length ;)

Upvotes: 0

Matthew
Matthew

Reputation: 15952

You have a spelling mistake in that code. It's length not lenght.

But aside from that, there's no need to call toString(). str.length is fine.

Upvotes: 1

Andrew Kloos
Andrew Kloos

Reputation: 4578

correct your spelling...

alert(str.toString().lengTH);

Andrew

Upvotes: 1

dlev
dlev

Reputation: 48596

You misspelled length. It's "length":

str.toString().length

You don't actually need the toString() call here, though. "1234" is already a string, so str.length works as well.

Upvotes: 7

Related Questions