DEV Community

PUSHAN VERMA
PUSHAN VERMA

Posted on

2 1

Strings in JavaScript and their Important Methods

Strings

Standard Approach
let topic ="verma this is my name -> vermaji ";

console.log(topic);

Image description

Concatination

let adname ="aditi verma "
topic =topic.concat("Pushan Verma ","chetna verma ",adname,"chandresh verma");
console.log(topic);
Image description

*character At method type in java *
console.log(topic[0]); // method to display the character present inside the string in js
Image description

indexOf,LastIndexOf,substring,slice,split,replace
console.log(topic);
console.log(topic.indexOf('verma')); // it will give the index of first character if it founds , otherwise it gives -1
console.log(topic.lastIndexOf('verma'));
console.log(topic.indexOf('Pushan'));
console.log(topic.substring(1,6));
console.log(topic.slice(-4)); // slice can give from back also , but substring cannot , if i write substring(-4 it will return the whole string )
console.log(topic.split(" "));
console.log(topic.replace("verma","rajput")); // it only changes the first occurence not all the occurences

Image description

Template Literals(Displaying js in HTml )
let fruit1="Banana";
let fruit2="Mango";

let content1=My favourite fruit is ${fruit1} and ${fruit2};

document.body.innerHTML=content1;

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay