P
P
Pavel2015-10-30 02:12:35
JavaScript
Pavel, 2015-10-30 02:12:35

Why does javascript do this?

What happens if you run this code?

name = "string"
name = name.split('');
name_spliced = name.splice(1,1);
console.log(name_spliced)

Do you think he'll get out ["t"]?
No, it will throw an error. Why? Because.
not_name = "string"
not_name = not_name.split('');
not_name_spliced = not_name.splice(1,1);
console.log(not_name_spliced)

But this one will give out after a ["t"]
little digging, I determined what name.split('')js could do, only then, for some reason, it became a string with commas, not an array.
In Crhome, there are such wonders: take.ms/vQCVx here it is first a string, then an array, and then a string again.
In IE EDGE this works take.ms/XKi5I

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
amatory10, 2015-10-30
@Carduelis

the point is that name is a property of the global window object, and it can only be a string;
when assigning any value, the toString () function is called.
The same situation in principle with status.

A
Alexander Aksentiev, 2015-10-30
@Sanasol

name = "string"
name[1]

S
Sergey Voronkov, 2015-10-30
@no_smoking

Always use the var keyword and you won't have these problems :)
var name = "string";
name = name.split('');
name_spliced ​​= name.splice(1,1);
console.log(name_spliced)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question