K
K
Kerrik Sun2021-06-21 14:58:05
JavaScript
Kerrik Sun, 2021-06-21 14:58:05

Object cannot be converted to another data type?

A question. Why does the operator typeofstill return , after converting an object to a primitive data type object?

let a = {
};
console.log(String(a)); //преобразование к строке
console.log(typeof a); //object


Does this mean that the conversion can only occur to hints, BUT not to data types (that is, hints can be different, but the data type is always the same - an object)?

Or does the conversion to other data types occur, and this is just a JS error (like, for example typeof function => function)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2021-06-21
@Eva25

String(a) returns the result of the transformation, but the original object remains unchanged.

const strA = String(a)
typeof strA // string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question