Friday, January 6, 2023

JavaScript String Methods

 // Strings

const certification = 'JavaScript Developer';
const type = 'Dev';

console.log(type[0]);
console.log(type[1]);
console.log(type[2]);

console.log(certification.length);
console.log('Dev'.length);

console.log(certification.indexOf('a'));
console.log(certification.lastIndexOf('a'));
console.log(certification.indexOf('Dev'));

console.log(certification.slice(11));
console.log(certification.slice(11,14));

console.log(certification.slice(0,certification.indexOf(' ')));
console.log(certification.slice(certification.lastIndexOf(' ') + 1));

console.log(certification.slice(-3));
console.log(certification.slice(1,-1));

// Lower Case, Upper Case and Trim
console.log(certification.toLowerCase());
console.log(certification.toUpperCase());
console.log(certification.toUpperCase().trim());

// Replacing 
const desc = 'I am a developer. I am working from 10 years.';
console.log(desc.replace('10','15').replaceAll('.',';'));
// Regular expression to replace all the occurances
console.log(desc.replace(/am/g,'was'));
// Regular expressions should in / and g stands for global
// Replace method is case sensitive 

// Booleans 
console.log(desc.includes('am'));
console.log(desc.includes('was'));
console.log(desc.startsWith('I'));
console.log(desc.endsWith('.'));

// Split 
console.log(desc.split(' '));
const [firstName, lastName] =  'Srinu SFDC'.split(' ');
// join
const myName = ['Mr.', firstName, lastName].join(' ');
console.log(myName);

// Padding 
console.log(myName.padStart(20,'*').padEnd(30,'*'));

const maskCard = function(cardNum) {
	const str = String(cardNum);
	const lastDigits = str.slice(-4);
	return lastDigits.padStart(str.length,'*');
}

console.log(maskCard(90989098564578765));

// Repeat 
console.log('Flight is dealyed for 1 hour.............'.repeat(6));

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...