DEV Community

PUSHAN VERMA
PUSHAN VERMA

Posted on

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;

Top comments (0)