JavaScript String length

Last Updated On
Saturday 9th Apr 2022

In this short tutorial, we learn about the JavaScript string length property.

The JavaScript String length property returns the number of characters in a string.

Syntax

The syntax to use the length property is as follows

	str.length
	
  • Here, str is a string.

Returns

  • The length property returns an integer value that represents the number of characters in the string.
  • If the string is an empty string, the length property will return 0.

Examples

	const helloWorld = "Hello World";

console.log(helloWorld.length)
	

OutPut

	11
	
	const emoji = '😇';

console.log(emoji.length);
	

OutPut

	2
	

The .length property will return for empty strings

	console.log("".length);
	

OutPut

	
// 0

	
  • You can even use it directly to any string you encounter
	console.log("My Name is Adam".length)
	

OutPut

	15
	

Notes:

  • The String.length property is a read-only property. Means if we try to change it manually there will be no effect.
  • The String.length property actually returns the code units in the UTF-16 string format.You Can use this link to read more about UTF-16