Skip to content

Instantly share code, notes, and snippets.

@GideonBabu
Last active January 28, 2020 08:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GideonBabu/62cc909930ce35c11acc8b9fb0c5ddd5 to your computer and use it in GitHub Desktop.
Save GideonBabu/62cc909930ce35c11acc8b9fb0c5ddd5 to your computer and use it in GitHub Desktop.
Useful JavaScript (JS) or jQuery snippets for day-to-day Software Development
// detect the Enter key in a text input field
$(".input1").on('keyup', function (e) {
if (e.keyCode === 13) {
// Do something
}
});
// array for..of cycle
// tip: you can stop iterating at any time using a break statement.
const colors = ['blue', 'green', 'white'];
for (const color of colors) {
console.log(color);
}
// 'blue'
// 'green'
// 'white'
/*
* for cycle
* tip: you can stop iterating at any time using a break statement.
*/
const colors = ['blue', 'green', 'white'];
for (let index = 0; index < colors.length; index++) {
const color = colors[index];
console.log(color);
}
// 'blue'
// 'green'
// 'white'
/*
* array.forEach() method
* tip: you cannot break array.forEach() iterating
*/
const colors = ['blue', 'green', 'white'];
colors.forEach(function callback(value, index) {
console.log(value, index);
});
// 'blue', 0
// 'green', 1
// 'white', 2
/*
* array.map() method
*/
const numbers = [0, 2, 4];
const newNumbers = numbers.map(function increment(number) {
return number + 1;
});
console.log(newNumbers)
// => [1, 3, 5]
/*
* Array.from() function
*/
const numbers = [0, 2, 4];
const newNumbers = Array.from(numbers,
function increment(number) {
return number + 1;
}
);
console.log(newNumbers);
// => [1, 3, 5]
/*
* array.reduce() method
*/
const numbers = [2, 0, 4];
function summarize(accumulator, number) {
return accumulator + number;
}
const sum = numbers.reduce(summarize, 0);
console.log(sum); // => 6
/*
* check-whether-a-string-contains-a-substring
*/
// ECMAScript 6
var string = "foo",
substring = "oo";
console.log(string.includes(substring));
// in ECMAScript 5 or older environments, use String.prototype.indexOf, which returns -1 when a substring cannot be found
var string = "foo",
substring = "oo";
console.log(string.indexOf(substring) !== -1);
// to break string by some value in it and make it as an array make use of string.prototype.split
// note: it doesn't change the string value and just output
let str = "https://www.google.com/a/b/c";
str.split('/'); // ["https:", "", "www.google.com", "a", "b", "c"]
// To add value at the front of the array use unshift
// array unshift in javascript function returns the new length or total number of items in the array in this case 4
let numbers = [2, 3, 4];
numbers.unshift(1)
console.log(numbers)
// javascript array shift
// The shift() method removes the first element from an array and returns that removed element.
// This method changes the length of the array.
const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment