JavaScript Tips, Tricks, and Best Practices

How to check whether a string contains a substring in JavaScript?

1. includes

const val1 = "abcoo";
const val2 = "oo";
console.log(string.includes(val2));

2. index of

var string = "foo";
var substring = "oo";
console.log(string.indexOf(substring) !== -1);

How to replace all occurrences of a string?

str = str.replace(/abc/g, '');
var find = 'abc';
var re = new RegExp(find, 'g');

str = str.replace(re, '');
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

How to correctly clone a JavaScript object?

var val1 = {data: "value"};
var val2= Object.assign({}, val1);

What is the !! (not not) an operator in JavaScript?

!!false === false
!!true === true

!!0 === false
!!parseInt("foo") === false // NaN is falsy
!!1 === true
!!-1 === true // -1 is truthy
!!(1/0) === true // Infinity is truthy

!!"" === false // empty string is falsy
!!"foo" === true // non-empty string is truthy
!!"false" === true // ...even if it contains a falsy value

!!window.foo === false // undefined is falsy
!!null === false // null is falsy

!!{} === true // an (empty) object is truthy
!![] === true // an (empty) array is truthy; PHP programmers beware!

How to Loop through an array in JavaScript

1. Sequential for loop:

var array = ["a","b"];
var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {
console.log(array[i]);
//Do something
}

2. Array.prototype.forEach

const data = ["a", "b", "c"]
data.forEach(function (item, index) {
console.log(item, index);
});

3. Use of reduce:

const numbers = [1,2,3,4,5];
const sum = numbers.reduce((total, n) => total + n, 0);
console.log(sum);

4. ES6 for-of statement

let data = ['a', 'b', 'c'];
for (const a of data){
console.log(a);
}

How do I copy to the clipboard in JavaScript?

function copy(text) {
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
}
<button id="data" onclick="copy(document.getElementById('data').innerHTML)">Copy here</button><script>
function copyToClipboard(text) {
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
}
</script>

How do I test for an empty JavaScript object?

1.jQuery:

jQuery.isEmptyObject({}); // true

2. lodash:

_.isEmpty({}); // true

3. Underscore:

_.isEmpty({}); // true

How do I make the first letter of a string uppercase in JavaScript?

p:first-letter {
text-transform:capitalize;
}
function makeCapital(s)
{
return s && s[0].toUpperCase() + s.slice(1);
}

Top Writer in Writing | Humor: Writing about Angular, JavaScript, and TypeScript related articles.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store