Wednesday, July 3, 2024
HomeLanguagesJavascriptJavaScript string.length

JavaScript string.length

The string.length is a property in JavaScript which is used to find the length of a given string. The string.length property returns 0 if the string is empty.

Syntax:

string.length

Parameter: It does not accept any parameter. 

Return Values: It returns the length of the given string.

JavaScript code to show the working of string.length property:

Example 1: This example returns the length of the strings using the string.length property of Javascript.

javascript




// Taking some strings
let x = 'neveropen';
let y = 'gfg';
let z = '';
 
// Returning the length of the string.
console.log(x.length);
console.log(y.length);
console.log(z.length);


Output:

13
3
0

Example 2: This example returns the length of the strings using the string.length property of Javascript.

javascript




// Taking some strings.
let x = '2341312134';
let y = '@#$%^&**((*&^';
 
// Variable z contains two spaces
let z = ' ';
 
// Returning the length of the string.
console.log(x.length);
console.log(y.length);
console.log(z.length);


Output:

10
13
2

Example 3 : In this example, the Array.from() method is used to convert the string “neveropen” into an array of individual characters. The length property is then used on the array to obtain the number of elements, which represents the length of the original string.

Javascript




const str = "neveropen";
const strArray = Array.from(str);
const length = strArray.length;
console.log(length);


Output

13

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browser:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Opera 3 and above
  • Safari 1 and above

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments