JavaScript Code examples

JavaScript Code examples

JavaScript String Methods Example Code snippets Learn JavaScript Strings 1
Source Code Template literals 2
Coding example String WhiteSpace Remover sample code 3
Remove duplicates from an array source code example 4

JavaScript String Methods Example Code snippets Learn JavaScript Strings

Code Example
const output = document.querySelector(‘.output’);

let str1 = ‘Hello world Hello World’;
let str2 = new String(str1);
console.log(str1.length);

for(let x=0;x<str1.length;x++){
//console.log(str1[x]);
//console.log(str1.charAt(x));
}

console.log(typeof str1);
console.log(typeof str2);

//console.log(eval(str1));
console.log(eval(str2));

console.log(str1.includes(‘Hello’));
console.log(str2.includes(‘Hello’,4));

let val = str2.indexOf(‘dwo’);
val = str2.lastIndexOf(‘or’);
val = str2.replace(‘or’,’WoW’);
val = str2.replaceAll(‘or’,’WoW’);
val = str1.split(‘o’);
val = str2.substring(2,-8);
console.log(val);
val = str2.substr(2,8);
val = str2.slice(2,-8);

console.log(val);
output.innerText = val;

Source Code Template literals
let val1 = Hello '"World;
let a = ‘Laurence Svekis’;
val1 = Hello ${a};
val1 = Adding ${5+10};
val1 = backtick \``; val1 =Line 1
Line 2; const b = 10; const c = 33; val1 =${b} + ${c} = ${b+c}`;

const first = ‘Laurence’;
const last = ‘Svekis’;
function fullName(greeting,fName,lName){
console.log(greeting);
console.log(fName,lName);
return ${greeting[0]} ${fName} ${lName};
}
val1 = fullNameWelcome${first}NEW${last}MIDDLE${last}END;

const game = {
level : 5,
name : ‘Laurence’
}

function checker(output,user,level){
const statusVal = level > 3 ? ‘pro’ : ‘beginner’;
console.log(output);
return ${output[0]} : ${user} is a ${statusVal} at level "${level}";
}

val1 = checkerPlayer${game.name}A${game.level}B;
game.level = 2;
val1 = checkerPlayer${game.name}${game.level};

document.querySelector(‘.output’).innerHTML = val1;
console.log(val1);

Coding example String WhiteSpace Remover sample code

String.prototype.cleaner = function(){
return this.replace(/\s+/g,’ ‘).trim();
}

const myStr1 = ” Hello World “;
console.log(myStr1.cleaner());

let val = myStr1;
val = trimMyString(val);
console.log(val);
document.querySelector(‘.output’).textContent = val;

function trimMyString(str){
let val = str.replace(/\s+/g,’ ‘).trim();
//val = str.trimLeft();
//val = str.trimRight();
return val;
}

Remove duplicates from an array source code example
Exercise :
Create an array and add values, include some duplicate values
Using the new Set() return the original array with only unique values, creating a new array
Assign one array to the original array, notice that they are connected. Any updates to one will update the other as well.
Assign a new array to the people1 array, using the new Set method returning only unique values from the original people array
Using the filter method to return unique items by checking for a match of the index value, duplicates will not match the index value since the indexOf returns the index value of the first matching result.

const people = [‘Laurence’,’Linda’,’Jane’,’Jack’,’Jack’];
for(let i=0;i<10;i++){
people.push(‘Laurence’);
}
let people1 = people;
console.log(people);
const newPeople = [… new Set(people)];
console.log(newPeople);
people1.push(‘Mike’);
console.log(people);
people1 = [… new Set(people)];
console.log(people1);
people1.push(‘New’);
console.log(people);
console.log(people1);
console.log(newPeople);

const people2 = people.filter((item,i)=> people.indexOf(item) === i);
console.log(people2);