JS String Methods

Method

Description

Example

Returns the length of the string

let str = "whereisstuff";
str.length // output 12

Returns the character index of the string

let str = "hello";
str[0] // output "h"
str.charAt(0) // output "h"

Returns the uppercase string

let str = "Hello";
str.toUpperCase(); // output "HELLO"

Returns the lowercase string

let str = "WORLD";
str.toLowerCase(); // output "world"

Returns the index of the first occurrence

let str = "JavaScript is fun";
str.indexOf("is"); // 11

Checks if the substring exists

let str = "JavaScript is fun";
str.includes("fun"); // true
str.includes("bun"); // false

Checks the start of string

let str = "JavaScript is fun";
str.startsWith("Java"); // true
str.startsWith("fun"); // false

Checks the end of string

let str = "JavaScript is fun";
str.endsWith("Java"); // false
str.endsWith("fun"); // true

slice string

let str = "JavaScript is fun";
str.slice(0, 4); // "Java"

Joins two or more strings

let str = "JavaScript";
str.concat(" is fun"); // "JavaScript is fun"

Removes whitespace

let str = "  JavaScript  ";
str.trim(); // "JavaScript"

Replaces a substring

let str = "JavaScript";
str.replace("Java", "Hello"); // "HelloScript"

Splits a string into an array

let str = "Java Script";
str.split(" "); // ["Java","Script"]

Repeats a string

let str = "Hi";
str.repeat(3); //HiHiHi 

Pads a string

let str = "5";
str.padStart(3, "0") // "0005"
str.padEnd(3, "0") // "5000"

Convert string to a number

let str = "5";
Number(str) // 5

Convert string to a number

let str = "5";
Number(str) // 5
let str2 = "20px";
Number(str2) // 20

Convert string to a float number

let str = "123.45";
parseFloat(str) // 123.45

length:

Returns the length of the string

Example
let str = "whereisstuff";
str.length // output 12

Try it yourself


Access Characters:

Returns the character index of the string

Example
let str = "Hello";
console.log(str[0]); // H
console.log(str.charAt(1)); // e

Try it yourself


Change Case:

toUppercase() → Returns the uppercase string

toLowercase() → Returns the Lowercase string

Example
console.log("Hello".toUpperCase()); // HELLO
console.log("WORLD".toLowerCase()); // world

Try it yourself


Searching Strings:

  • indexOf() → Returns the index of the first occurrence
  • includes() → Checks if the substring exists
  • startsWith() / endsWith() → Checks the start/end
Example
let str = "JavaScript is fun";
console.log(str.indexOf("is")); // 11
console.log(str.includes("fun")); // true
console.log(str.startsWith("Java")); // true
console.log(str.endsWith("fun")); // true

Try it yourself


Extracting Parts of a String:

  • slice(start, end)
  • substring(start, end)
Example
let str = "JavaScript";
console.log(str.slice(0, 4)); // Java
console.log(str.substring(0, 4)); // Java

Try it yourself


String Manipulation:

  • concat() → Joins two or more strings
  • trim() → Removes whitespace
  • replace() → Replaces a substring
  • split() → Splits a string into an array
Example
let str = " Hello World ";
console.log(str.trim()); // "Hello World"
console.log(str.replace("World", "JavaScript")); // " Hello JavaScript "
console.log(str.split(" ")); // ["", "Hello", "World", ""]

Try it yourself


Repeat and Pad:

  • repeat() → Repeats a string
  • padStart() / padEnd() → Pads a string
Example
console.log("Hi".repeat(3)); // HiHiHi
console.log("5".padStart(3, "0")); // 005
console.log("5".padEnd(3, "0")); // 500

Try it yourself


String to Number Conversion

  • Number() → Convert string to a number
  • parseInt() / parseFloat()
Example
let str = "123";
console.log(Number(str)); // 123
console.log(parseInt(str)); // 123
console.log(parseFloat("123.45")); // 123.45

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.