Sep 21, 2020

Javascript | Basic String methods Examples in a Nutshell

Below are summarized String methods with examples and output used in JavaScript in a Nutshell:

// String methods

var a10;
var b20;
console.log(a+b); //30
var str1 = "Hello";
var str2 = "World";
console.log(str1+str2); //HelloWorld
console.log(str1+str2+a+b); //HelloWorld1020
console.log(a+b+str1+str2); //30HelloWorld

var s1 = "Hey You can call me \"Ishan!\"";
console.log(s1); //Hey You can call me "Ishan!"
var s1 = 'Hey You can call me "Ishan!"';
console.log(s1); //Hey You can call me "Ishan!"

// String bracket Notation[]:
var firstName = "Ishan";
console.log(firstName[0]); //I
// charAt()
console.log(firstName.charAt(4)); //n
var str1 = "Welcome to Learn Automation";
var str2"On both Web and API";
console.log(str1.charAt(8)); //Space is also included
// charCodeAt()
console.log(str1.charCodeAt(8)); //116
console.log(str1.concat(" ").concat(str2)); 
// Welcome to Learn Automation On both Web and API
// endsWith()
console.log(str1.endsWith("ion")); //true
console.log(str1.endsWith("abc")); //false
// includes()
console.log(str1.includes("Learn")); //true
console.log(str1.indexOf("Learn")); //11
var str3 = "End to End Testing End";
console.log(str3.match(/End/g)); //[ 'End', 'End', 'End' ]
//End to End Testing EndEnd to End Testing EndEnd to End Testing End
console.log("to")); //gives result of 1st occurence >>> 8
console.log(str3.slice(3)); //to End Testing End
console.log(str3.slice(2,6)); //d to
console.log(str3.substr(3)); //to End Testing End
console.log(str3.substr(2,6)); //d to E
var str = "          Hi! I'm Ishan           ";
console.log(str.trim()); //Hi! I'm Ishan

No comments:

Post a Comment

Thanks a lot for your valuable Comment!

Featured this week

Latest Employment BUZZ: 4 working days in a week?

COVID-19 has hit mostly all sectors including IT  too bad. If we think otherwise, on the other hand,  there have been many new technical inn...