Javascript String

Javascript String length

Javascript String Object, which is one of the native object , is a wrapper class for all strings. The String object has a few properties and methods, most of which it inherits from Object.

String Length

The String.length property returns an integer value that is the result of total number of characters in the string.

Syntax

example

Above script will alert "Length is 16"

trim() method

Javascript String trim() method

Javascript string trim() method returns a new string with all leading and trailing whitespaces removed from the original string. The original string is not changed.

Syntax

example

output

Javascript string trim method

In the above output, you can see the leading and trailing whitespaces removed from the original string

String trimLeft()

Javascript String.trimLeft() method returns a new string with all leading whitespaces removed. The original string is not changed.

Syntax

example

output

javascript string trimleft

In the above output, you can see the leading whitespaces (left side spaces) removed from the original string

String trimRight()

Javascript String.trimRight() method returns a new string with all trailing whitespaces removed. The original string is not changed.

Syntax

example

Output:

javascript trim right

In the above output, you can see the trailing whitespaces (right side spaces) removed from the original string

String toLowerCase()

Javascript String toLowerCase

Javascript String toLowerCase() method returns the value of the string with all characters converted to lowercase. It will not affect the value of the original string itself.

Syntax

example

When you run the above script, the output is "this is a string".

String toUpperCase()

Javascript String toUpperCase() method returns the value of the string with all characters converted to uppercase. It will not affect the value of the original string itself.

Syntax

example

When you run the above script, the output is "THIS IS A STRING".