Skip to main content

String length JavaScript

In javascript using length property we can find the length of a string.

The length property returns the length of a string (number of characters).

For example:

const myString = "Hello, CSC!";
console.log(myString.length); // Output: 20

There are many methods available for manipulating strings in JavaScript. Let's explore some of the most commonly used string methods one by one.

charAt()

Returns the character at a specified index in the string.

const myString = "Hello, CSC!";
console.log(myString.charAt(0)); // Output: H

concat()

Joins two or more strings together.

const myString = "Hello";
const anotherString = ", CSC!";
console.log(myString.concat(anotherString)); // Output: Hello, CSC!

indexOf()

Returns the index of the first occurrence of a specified substring in the string.

const myString = "Hello, CSC!";
console.log(myString.indexOf("CSC")); // Output: 7

slice()

Extracts a portion of the string and returns it as a new string.

const myString = "Hello, CSC!";
console.log(myString.slice(7)); // Output: CSC!

toUpperCase()

Converts the string to uppercase.

const myString = "Hello, CSC!";
console.log(myString.toUpperCase()); // Output: HELLO, CSC!

toLowerCase()

Converts the string to lowercase.

const myString = "Hello, CSC!";
console.log(myString.toLowerCase()); // Output: hello, CSC!

split()

Splits a string into an array of substrings based on a specified delimiter.

const myString = "Hello, CSC!";
console.log(myString.split(",")); // Output: ['Hello', ' CSC!']

replace()

Replaces a substring in the string with another substring.

const myString = "Hello, CSC!";
console.log(myString.replace("CSC", "UpSkillers")); // Output: Hello, UpSkillers!

Template literals

A syntax for creating strings that allows for variable interpolation and multi-line strings.

Template literals are enclosed in backticks "(`)" and variables are wrapped in "${}".

const name = "CSC";
console.log(`Hello, ${name}!
How are you today?`);
// Output: Hello, CSC!
// How are you today?

String methods that return a boolean value

startsWith(), endsWith(), and includes() are the methods that return a boolean value.

These methods can be used to check if a string starts, ends, or includes a specified substring.

const myString = "Hello, CSC!";
console.log(myString.startsWith("Hello")); // Output: true
console.log(myString.endsWith("CSC!")); // Output: true
console.log(myString.includes("UpSkillers")); // Output: false

JavaScript String trim()

The trim() method in JavaScript is used to remove whitespace (spaces, tabs, and line breaks) from both ends of a string.

It returns a new string with the whitespace removed. The original string is not modified.

Example of how to use trim():

const myString = '   Hello, CSC!   ';
console.log(myString.trim()); // Output: 'Hello, CSC!'

String Methods - Example

Editor

Loading...
info

JavaScript strings are immutable, meaning that once a string is created, it cannot be changed. However, the methods provided by JavaScript can be used to manipulate strings and create new strings based on existing ones.