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.
Copied
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:
Copied
String.length
length example 1:
create a string declaration with sentence and try to find its length.
Copied
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:
- Copy the above example code.
- Open the browser(chrome or Microsoft edge) and press(ctrl+shift+I).
- Go to the console tab.
- 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.
Copied
let str='';
console.log('declared string = '+str);
console.log('declared string length = '+str.length);
Execution Steps:
- Copy the above example code.
- Open the browser(chrome or Microsoft edge) and press(ctrl+shift+I).
- Go to the console tab.
- Paste the copied code into console and press enter button.
Output:
declared string =
declared string length = 0