JavaScript - String length

In this post, we will discuss about string length in JavaScript. length is a data property of the string stores number of characters length of the string.

Let us create the String for demonstration.

CopiedCopy Code
let str='I like the JavaScript and JavaScript is popular and easy learn and gets job easily with JavaScript skill.';
console.log('declared string = '+str);

Output:

declared string = I like the JavaScript and JavaScript is popular and easy learn and gets job easily with JavaScript skill.

String length:

length used to find the number of characters contains in a string.

length syntax:

CopiedCopy Code
String.length

length example 1:

create a string declaration with sentence and try to find its length.

CopiedCopy Code
let str='I like the JavaScript and JavaScript is popular and easy learn and gets job easily with JavaScript skill.';
console.log('declared string = '+str);
console.log('declared string length = '+str.length);

Execution Steps:

  1. Copy the above example code.
  2. Open the browser(chrome or Microsoft edge) and press(ctrl+shift+I).
  3. Go to the console tab.
  4. Paste the copied code into console and press enter button.

Output:

declared string = I like the JavaScript and JavaScript is popular and easy learn and gets job easily with JavaScript skill.
declared string length = 105

length example 2:

In this example, we will try to find the length of a empty string.

CopiedCopy Code
let str='';
console.log('declared string = '+str);
console.log('declared string length = '+str.length);

Execution Steps:

  1. Copy the above example code.
  2. Open the browser(chrome or Microsoft edge) and press(ctrl+shift+I).
  3. Go to the console tab.
  4. Paste the copied code into console and press enter button.

Output:

declared string = 
declared string length = 0