开发者

JavaScript calculating length of a string

开发者 https://www.devze.com 2023-03-30 08:52 出处:网络
Why when I try this block of code: var str = \"1234\"; alert(str.toString().lenght); it alerts开发者_C百科 me \'undefined\' ?You misspelled length. It\'s \"length\":

Why when I try this block of code:

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

it alerts开发者_C百科 me 'undefined' ?


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.


correct your spelling...

alert(str.toString().lengTH);

Andrew


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.


Just a typo: it's .length ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消