String.prototype.toString()
O método toString()
retorna uma string representando o objeto especificado.
Sintaxe
str.toString()
Descrição
O objeto String
substitui o método toString()
do objeto Object
. Ele não herda Object.prototype.toString()
. Para objetos String
, o método toString()
retorna uma representação de string do objeto e é o mesmo que o método String.prototype.valueOf()
.
Exemplos
Usando toString()
O exemplo a seguir exibe o valor string de um objeto String
:
var x = new String('Hello world');
console.log(x.toString()); // retorna 'Hello world'
Especificações
Especificação | Estado | Comentário |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Padrão | Definição inicial. Implementado no JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262) The definition of 'String.prototype.toString' in that specification. |
Padrão | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.prototype.toString' in that specification. |
Padrão | |
ECMAScript (ECMA-262) The definition of 'String.prototype.toString' in that specification. |
Padrão em tempo real |
Navegadores compatíveis
BCD tables only load in the browser