javascript string length trim, trimLeft, trimRight, toLowerCase and toUpperCase Best 5 Example

javascript string length: Strings are a crucial part of all programming languages. here we can learn to string.length : returns the number of total characters in a string with best 5 Examples with demo.

Best Javascript String Functions

  • Get String Length
  • trim() method
  • String trimLeft()
  • String trimRight()
  • String toLowerCase()
  • String toUpperCase()

javascript string length

In JavaScript, the syntax for javascript string length is:

string.length;

Example : 1 Interesting Fact

var live_str = 'Pakainfowebsite';

console.log(live_str.length);

JavaScript String length Property

index.html

<!DOCTYPE html>
<html>
<body>

<button onclick="getNumerOfCharcters()">Try it</button>

<p id="example"></p>

<script>
function getNumerOfCharcters() {
  var data = "Welcome To Pakainfo.com!";
  var count = data.length;
  document.getElementById("example").innerHTML = count;
}
</script>

</body>
</html>

Results:

24

don’t Miss : String methods in JavaScript

Read Also:  jquery class selector - How to use jQuery Class and Id Selector to find DOM elements?

How TO – Get The Length of a String?

var str = "AB98CDEF256GHIJ45841KL6024485MNOPQR98964STUV56875WXYZ";
var len = str.length;

Finding Number of Characters in a String

let user_str = "";
let len = user_str.length;
console.log(len); // 0

user_str = "I love Pakainfo.";
len = user_str.length;
console.log(len); // 16

user_str.length = 5;
console.log(user_str); // I love Pakainfo.

Output

0
16
I love Pakainfo.

Example : 2 Javascript String Length

let user_str = "Pakainfo.com is great.";

if (user_str.length <= 20) {
 console.log("user_str is a short string");
} else {
 console.log("user_str is a long string");
}

print Line by line

for (let i = 0; i < user_str.length; i++) {
 console.log(user_str[i].toUpperCase());
}

Javascript string length, string trim

trim() method

trim() method : syntax

string.trim();

Example

	var movie_site = " It is a tamilblasters ";
	document.write("$" + movie_site + "$" + "<br>");
	document.write("$" + movie_site.trim() + "$");

String trimLeft()

syntax

string.trimLeft();

Example

var movie_site = " It is a tamilblasters ";
document.write("$" + movie_site + "$" + "<br>");
document.write("$" + movie_site.trimLeft() + "$");

String trimRight()

syntax

string.trimRight();

Example

var movie_site = " It is a tamilblasters ";
document.write("$" + movie_site + "$" + "<br>");
document.write("$" + movie_site.trimRight() + "$");

don’t Miss : JavaScript String Methods with Example

Read Also:  jQuery Add Remove Class on mouseOver mouseOut

String toLowerCase()

syntax

string.toLowerCase()

Example

var movie_site = " It is a tamilblasters ";
	document.write(movie_site.toLowerCase());

String toUpperCase()

syntax

string.toUpperCase()

Example

var movie_site = " It is a tamilblasters ";
	document.write(movie_site.toUpperCase());

I hope you get an idea about javascript string length.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.