Can we say that String is an object in Javascript?

前端 未结 4 1208
春和景丽
春和景丽 2021-01-19 04:24

I\'m always confused when I hear strings are primitives in JS, because everybody knows that string has different methods like: length, indexOf, search etc.

l         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-19 05:05

    Strings are not objects, they are native types, like numbers, but if you want to access the method on it they are boxed with String object. The same happen with numbers you can call (10).toString() but in fact you're calling toString on Number instance that wraps 10, when you call the method.

提交回复
热议问题