Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

String.length

Resum

La propietat length representa la longitud d'una cadena de caràcters.

Sintaxi

str.length

Descripció

Aquesta propietat retorna el nombre d'unitats de codi en un string. UTF-16, el format de string utilitzar a JavaScript, utilitza un únic codi de 16 bits per a representar els caràcters més comuns, però necessita dos unitats de codi per a caràcters menys comuns, així que és possible que el valor retornat per length no encaixi amb el nombre de caràcters reals a la cadena de caràcters.

Per a una cadena buida, length és 0.

La propietat estàtica String.length retorna el valor 1.

Exemples

Exemple: Ús bàsic

var x = 'Mozilla';
var empty = '';

console.log('Mozilla té ' + x.length + ' unitats de codi');
/* "Mozilla té 7 unitats de codi" */

console.log('La string buida té una longitud de ' + empty.length);
/* "La string buida té una longitud de 0" */

Especificacions

Especificacions Estat Comentaris
ECMAScript 1a Edició. Standard Definició inicial. Implementat a JavaScript 1.0.
ECMAScript 5.1 (ECMA-262)
The definition of 'String.prototype.length' in that specification.
Standard  
ECMAScript 6 (ECMA-262)
The definition of 'String.prototype.length' in that specification.
Release Candidate  

Compatibilitat amb navegadors

Característica Chrome Firefox (Gecko) Internet Explorer Opera Safari
Suport bàsic (Yes) (Yes) (Yes) (Yes) (Yes)
Característica Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Suport bàsic (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

Vegeu també

Document Tags and Contributors

 Contributors to this page: teoli, enTropy
 Last updated by: teoli,