Javascript tips
1. Search/Find
Let’s look at how we can find a particular element in all the four built-in javascript objects for different use-cases.
Array
// array of objects array.find(object => object.id === 2); // returns object with id 2
//array of numbers starting from "zero" array.indexOf("one"); // returns 1 as index
Object
// array of Objects // eg: [{id: 1, name: "one"},...] can be converted to {1: {name: "one"}, ... } object[2] // returns the value of key 2 (i.e {name: "two"}
Note: When using n-operations to find an object, its always best to use object key for retrieving the item rather than array find.
Set
Sets have no built-in function to retrieve or find the index of its items even-though its an iterable, so ideally we would have to convert it to an array before indexOf/find operation.
const mySet = new Set(['1', '2', '3']); [...mySet].indexOf('2') // returns 1
const mySet = new Set([{1: 'one'}, {2: 'two'}, {3: 'three'}]); [...mySet].find(object => object[2] === 'two'); // returns {2: 'two'}
Map
Maps are special objects per se, they are iterables with key value pair constructor that looks like a 2D array but acts like an object. They offer a better flexibility in terms of choosing our key values. A map can have a key value which can be a string, number, object or even NaN.
var map = new Map([[ 1, 'one' ],[ 2, 'two' ]]); map.get(1) // returns 'one'
Note: Maps can be very flexible in places where objects can be a bit annoying, and it strongly serves the purpose in some specific scenarios, like adding and deleting key-pairs frequently.
2. Sort
Sort operations can be interesting, and most of the times we assume that sort over an iterable entity works out of the box. Well, it doesn’t always.
Array
Array sorts are often misunderstood by both beginners and intermediate developers. Since array’s default sort sorts an array based on Unicode
, we cannot expect to get same sort behaviour for all the datatypes. Hence, we often need to pass a comparator function into the sort.
// array of strings in a uniform case without special characters const arr = [ "sex", "age", "job"]; arr.sort(); //returns ["age", "job", "sex"]
// array of numbers const arr = [ 30, 4, 29 , 19]; arr.sort((a, b) => a-b); // returns [4, 19, 29, 30]
// array of number strings const arr = [ "30", "4", "29" , "19" ]; arr.sort((a, b) => a-b); // returns ["4", "19", "29", "30"]
// array of mixed numerics const arr = [ 30, "4", 29 , "19" ]; arr.sort((a, b) => a-b); // returns ["4", "19", 29, 30]
// array of non-ASCII strings and also strings
const arr = ['réservé', 'cliché', 'adieu'];
arr.sort((a, b) => a.localeCompare(b)); // returns is ['adieu', 'cliché','réservé']
// array of objects const arr = [ { name: 'Sharpe', value: 37 }, { name: 'And', value: 45 }, { name: 'The', value: -12 } ]; // sort by name string arr.sort((a,b) => a['name'].localeCompare(b['name']));
// sort by value number arr.sort((a,b) => a['value']-b['value']);
Note: I usually prefer using localeCompare to sort my strings as they offer more flexibility of controlling case-sensitivity, accent and also give us the benefit of considering language during the sort.
Object
There is no built-in method for the sorting of the objects, but ES6 offers some interesting built-in key-sorting during the creation of the object. Object keys are sorted only based on numerics/numeric-strings, all the other keys are pushed right after the numeric keys unsorted.
// object with numeric/numeric-string keys are sorted const obj = { 30: 'dad', '4': 'kid', 19: 'teen', '100': 'grams'}; console.log(obj) // returns {4: "kid", 19: "teen", 30: "dad", 100: "grams"} with sorted keys
// object with key-values as alpha-strings are not sorted const obj = { "b": "two", "a": "one", "c": "three" }; console.log(obj) // returns {b: "two", a: "one", c: "three"}
// object with numeric, numeric-string and alpha keys are partially sorted. (i.e only numeric keys are sorted) const obj = { b: "one", 4: "kid", "30": "dad", 9: "son", a: "two" }; console.log(obj) // returns {4: "kid", 9: "son", 30: "dad", b: "one", a: "two"}
Set
Sets do not have built-in sort functionality, however the easiest way to sort a set is to convert it to an array and implementing array’s sort method. Since, set is an iterable object, we can build our own sorting algorithm of our choice.
// set to array and array sort const set = new Set(['b', 'a', 'c']); [...set].sort(); // returns ['a', 'b', 'c'] which is an array
// alternatively we can use entries to sort a set and create a new sorted set. The iterator gives us the ['a', 'a'] when spread over an array. const set = new Set(['b', 'a', 'c']); const sortedSet = new Set([...set.entries()].map((entry) => entry[0]).sort());
Note: Keep in mind that sort method is of array’s and you would have to use a comparator appropriately to get your desired sort.
Map
Similar to sets maps do not have a built-in method themselves, but we can still spread their entries over an array and build a new sorted map.
// entries spread over an array can be sorted like an array const map = new Map([["c", 'three'],["a", 'one'], ["b", 'two']]); const sortedMap = new Map([...map.entries()].sort()) // returns sorted Map(3) {"a" => "one", "b" => "three", "c" => "two"}
Note: In the map sorting, it is important to know that the two-dimensional array from the map gets sorted based on the first element in each sub-array. Here the sorting is based on “a”, “b” and “c” strings. If these were numbers, you would have to use a comparator.
3. Includes or Has
One of the most important features of the iterable objects is to check the presence of the desired item. Almost, all of the built-in standard and iterable javascript objects have their own implementation to achieve this. Let’s look at them below.
Array
// we are considering a linear array only const arr = [1, 2, 3]; arr.includes(1); // returns true arr.includes('1'); // returns false as types do not match
Object
// we are going to consider only the keys const obj = { a: 1, b: 2, c: 3, 1: 'one' }; obj.hasOwnProperty('a'); // returns true obj.hasOwnProperty('1'); // returns true because no type check obj.hasOwnProperty(1); // returns true
Set
Set has a handy ‘has’ function which can be more efficient in accessing the values compared to an array.
const set = new Set([1, 2, 3, 4, 5]); set.has(4); // returns true set.has('4'); // returns false because of mismatch in type
Map
Map has a built-in ‘has’ function too.
const map = new Map([[3, 'three'],["a", 'one'], ["b", 'two']]); map.has('a'); // returns true map.has(3); // returns true map.has('3'); // returns false because types don't match
Note: Compared to the array’s includes function, Object’s hasOwnProperty and Set/Map’s has functions seem to perform close to O(1) in different tests, clearly more efficient in terms of larger data sets.
4. Removing Duplicates
There is no straight forward way to remove duplicates in a collection, given that array or object is linear, we can use some of the built-in methods to remove duplicates.
Array
There are many online methods to remove duplicates from an array. That this stackoverflow thread covers. However in ES6 let’s look at the easiest ways of removing duplicates from an array.
// considering a linear array Set gives us the answer we need. const arr = [1, 2, 2, 4, 5, 5]; [...new Set(arr)]; // returns [1, 2, 4, 5]
// however set doesn't remove duplicates from array of objects const arr = [{a:1},{b:2},{a:1}]; [...new Set(arr)]; // returns [{a:1},{b:2},{a:1}]
// hence we can use ES6 filter and map functions to achieve the same arr.filter((obj, index) => { return arr.map(obj => obj['a']).indexOf(obj['a']) === index; }); // removes duplicate based on the key 'a'
Note: Set will remove the duplicate objects if they are stringified and the strings match, but we still have to parse then back to the object once set is converted back to an array. The whole process might not be performant.
Object
Objects do not allow duplicate key values, old values are overwritten by the new values.
const obj = { b: "one", a: "two", a: "three" }; console.log(obj); // returns {b: "one", a: "three"}
Set
Sets inherently do not allow duplicate values when they are passed a linear iterable object like an array, but when they are passed an array of object they do allow duplicate objects.
// a linear array iterable const set = new Set([1, 2, 2, 4, 5, 5]); console.log(set); // returns Set {1, 2, 4, 5}
// array of objects const set = new Set([{a:1},{b:2},{a:1}]); console.log(set); // returns Set {{a:1},{b:2},{a:1}} with duplicate objects
Map
Maps also do not allow duplicate keys during the creation.
const map = new Map([[3, 'three'], [2, 'two'], [2, 'four']]); console.log(map); // returns {3 => "three", 2 => "four"}
5. Delete
Array
Array has no built-in method to delete its items. However we can use couple of methods to do it. Splice, indexOf or filter.
// I personally prefer this method. const arr = [ 'a', 'b', 'c' ]; arr.filter(e => e !== 'c'); // returns [ 'a', 'b' ] removing 'c'
Object
Objects do not have a built-in delete method, but according to the docs we can use the delete
keyword to delete a key. However, this is widely discouraged in the javascript community and even libraries like underscore and lodash use a different method.
// The infamous delete method const obj = { b: "one", a: "two" }; delete obj.a; // deletes a and returns true
Set
Set offers a built-in delete method making our lives easier
const set = new Set([1, 2, 4, 5]); set.delete(4); // deletes 4 and returns true set.delete('5'); // returns false as types do not match
Map
Map has its own built-in delete method to remove keys from a given map object.
const map = new Map([[3, 'three'], [2, 'two']); map.delete(3); // deletes [3, 'three'] and returns true. map.delete('2'); // returns false as types do not match
6. Length and Size
// arrays have a built-in property for length which is different from collection size. ['1', '2', '3'].length // returns 3
// objects have no built-in property to check length or size, so we need to resort to using keys array to check length. Object.keys({ b: 'one', a: 'two', c: 'three' }).length // returns 3
// set has a size property built-in new Set([{a:1},{b:2},{a:1}]).size // returns 3
// map has a size property built-in new Map([[3, 'three'],['a', 'one'], ['b', 'two']]).size // returns 3
Conclusion
These are some of the key pointers to remember when choosing the standard built-in objects of your choice. Though javascript offers us the flexibility of using more than one built-in objects, its always better to choose the one that suits the best. Feel free to leave your comments below.
Differents between the following two functions
var functionOne = function() {
// Some code
};
function functionTwo() {
// Some code
}
Answer:
// TypeError: functionOne is not a function if it is called before the defined posotion
functionOne();
var functionOne = function() {
console.log("Hello!");
};
// Outputs: "Hello!" if it is called before the defined posotion
functionTwo();
function functionTwo() {
console.log("Hello!");
}
QS:Functions and methods both are functions in JavaScript. A method is just a function which is a property of an object.?show an example
- function fun(param1, param2){
- // some code…
- }
The above code is an example of function.
- var obj = {
- name : “John snow”,
- work : function someFun(paramA, paramB) {
- // some code..
- }
- }
- fun(2,4);
- obj.work(2,4);
Qs:What is the output total X off the following
var x=0;
for(i=0;i<5;i++){
console.log("x"+x);
}
console.log("Toala X"+x);
Answer:5
Hackerranks:
QS:
Lilah has a string, , of lowercase English letters that she repeated infinitely many times.
Given an integer, , find and print the number of letter a
‘s in the first letters of Lilah’s infinite string.
For example, if the string and , the substring we consider is , the first characters of her infinite string. There are occurrences of a
in the substring.
Function Description
Complete the repeatedString function in the editor below. It should return an integer representing the number of occurrences of a
in the prefix of length in the infinitely repeating string.
repeatedString has the following parameter(s):
- s: a string to repeat
- n: the number of characters to consider
ANSWER:
function repeatedString(s, n) {
splice()
changes the original array whereas slice()
doesn’t but both of them returns array object.
See the examples below:
var array=[1,2,3,4,5];
console.log(array.splice(2));
This will return [3,4,5]
. The original array is affected resulting in array
being [1,2]
.
var array=[1,2,3,4,5]
console.log(array.slice(2));
QS:Array filter
function isEven(value) {
return value % 2 == 0;
}
var filtered = [11, 98, 31, 23, 944].filter(isEven);
print(filtered);
Output:
[98,944]
QS:How to use settimeout function to call another function
setTimeout(function() {
postinsql(topicId);
}, 4000);
setTimeout(postinsql.bind(null, topicId), 4000);
JavaScript Clean Coding Best Practices
“Even bad code can function. But if the code isn’t clean, it can bring a development organization to its knees.” — Robert C. Martin (Uncle Bob)
Node.js at Scale is a collection of articles focusing on the needs of companies with bigger Node.js installations and advanced Node developers. Chapters:
Click to see all chapters of Node.js at Scale:
First of all, what does clean coding mean?
Clean coding means that in the first place you write code for your later self and for your co-workers and not for the machine.
Your code must be easily understandable for humans.
You know you are working on a clean code when each routine you read turns out to be pretty much what you expected.
JavaScript Clean Coding Best Practices
Now that we know what every developer should aim for, let’s go through the best practices!
How should I name my variables?
Use intention-revealing names and don’t worry if you have long variable names instead of saving a few keyboard strokes.
If you follow this practice, your names become searchable, which helps a lot when you do refactors or you are just looking for something.
// DON'T
let d
let elapsed
const ages = arr.map((i) => i.age)
// DO
let daysSinceModification
const agesOfUsers = users.map((user) => user.age)
Also, make meaningful distinctions and don’t add extra, unnecessary nounsto the variable names, like its type (hungarian notation).
// DON'T
let nameString
let theUsers
// DO
let name
let users
Make your variable names easy to pronounce, because for the human mind it takes less effort to process.
When you are doing code reviews with your fellow developers, these names are easier to reference.
// DON'T
let fName, lName
let cntr
let full = false
if (cart.size > 100) {
full = true
}
// DO
let firstName, lastName
let counter
const MAX_CART_SIZE = 100
// ...
const isFull = cart.size > MAX_CART_SIZE
In short, don’t cause extra mental mapping with your names.
How should I write my functions?
Your functions should do one thing only on one level of abstraction.
Functions should do one thing. They should do it well. They should do it only. — Robert C. Martin (Uncle Bob)
// DON'T
function getUserRouteHandler (req, res) {
const { userId } = req.params
// inline SQL query
knex('user')
.where({ id: userId })
.first()
.then((user) => res.json(user))
}
// DO
// User model (eg. models/user.js)
const tableName = 'user'
const User = {
getOne (userId) {
return knex(tableName)
.where({ id: userId })
.first()
}
}
// route handler (eg. server/routes/user/get.js)
function getUserRouteHandler (req, res) {
const { userId } = req.params
User.getOne(userId)
.then((user) => res.json(user))
}
After you wrote your functions properly, you can test how well you did with CPU profiling – which helps you to find bottlenecks.
Use long, descriptive names
A function name should be a verb or a verb phrase, and it needs to communicate its intent, as well as the order and intent of the arguments.
A long descriptive name is way better than a short, enigmatic name or a long descriptive comment.
// DON'T
/**
* Invite a new user with its email address
* @param {String} user email address
*/
function inv (user) { /* implementation */ }
// DO
function inviteUser (emailAddress) { /* implementation */ }
Avoid long argument list
Use a single object parameter and destructuring assignment instead. It also makes handling optional parameters much easier.
// DON'T
function getRegisteredUsers (fields, include, fromDate, toDate) { /* implementation */ }
getRegisteredUsers(['firstName', 'lastName', 'email'], ['invitedUsers'], '2016-09-26', '2016-12-13')
// DO
function getRegisteredUsers ({ fields, include, fromDate, toDate }) { /* implementation */ }
getRegisteredUsers({
fields: ['firstName', 'lastName', 'email'],
include: ['invitedUsers'],
fromDate: '2016-09-26',
toDate: '2016-12-13'
})
Reduce side effects
Use pure functions without side effects, whenever you can. They are really easy to use and test.
// DON'T
function addItemToCart (cart, item, quantity = 1) {
const alreadyInCart = cart.get(item.id) || 0
cart.set(item.id, alreadyInCart + quantity)
return cart
}
// DO
// not modifying the original cart
function addItemToCart (cart, item, quantity = 1) {
const cartCopy = new Map(cart)
const alreadyInCart = cartCopy.get(item.id) || 0
cartCopy.set(item.id, alreadyInCart + quantity)
return cartCopy
}
// or by invert the method location
// you can expect that the original object will be mutated
// addItemToCart(cart, item, quantity) -> cart.addItem(item, quantity)
const cart = new Map()
Object.assign(cart, {
addItem (item, quantity = 1) {
const alreadyInCart = this.get(item.id) || 0
this.set(item.id, alreadyInCart + quantity)
return this
}
})
Organize your functions in a file according to the stepdown rule
Higher level functions should be on top and lower levels below. It makes it natural to read the source code.
// DON'T
// "I need the full name for something..."
function getFullName (user) {
return `${user.firstName} ${user.lastName}`
}
function renderEmailTemplate (user) {
// "oh, here"
const fullName = getFullName(user)
return `Dear ${fullName}, ...`
}
// DO
function renderEmailTemplate (user) {
// "I need the full name of the user"
const fullName = getFullName(user)
return `Dear ${fullName}, ...`
}
// "I use this for the email template rendering"
function getFullName (user) {
return `${user.firstName} ${user.lastName}`
}
#### Query or modificationFunctions should either do something (modify) or answer something (query), but not both.
Everyone likes to write JavaScript differently, what to do?
As JavaScript is dynamic and loosely typed, it is especially prone to programmer errors.
Use project or company wise linter rules and formatting style.
The stricter the rules, the less effort will go into pointing out bad formatting in code reviews. It should cover things like consistent naming, indentation size, whitespace placement and even semicolons.
The standard JS style is quite nice to start with, but in my opinion, it isn’t strict enough. I can agree most of the rules in the Airbnb style.
How to write nice async code?
Use Promises whenever you can.
Promises are natively available from Node 4. Instead of writing nested callbacks, you can have chainable Promise calls.
// AVOID
asyncFunc1((err, result1) => {
asyncFunc2(result1, (err, result2) => {
asyncFunc3(result2, (err, result3) => {
console.lor(result3)
})
})
})
// PREFER
asyncFuncPromise1()
.then(asyncFuncPromise2)
.then(asyncFuncPromise3)
.then((result) => console.log(result))
.catch((err) => console.error(err))
Most of the libraries out there have both callback and promise interfaces, prefer the latter. You can even convert callback APIs to promise based one by wrapping them using packages like es6-promisify.
// AVOID
const fs = require('fs')
function readJSON (filePath, callback) {
fs.readFile(filePath, (err, data) => {
if (err) {
return callback(err)
}
try {
callback(null, JSON.parse(data))
} catch (ex) {
callback(ex)
}
})
}
readJSON('./package.json', (err, pkg) => { console.log(err, pkg) })
// PREFER
const fs = require('fs')
const promisify = require('es6-promisify')
const readFile = promisify(fs.readFile)
function readJSON (filePath) {
return readFile(filePath)
.then((data) => JSON.parse(data))
}
readJSON('./package.json')
.then((pkg) => console.log(pkg))
.catch((err) => console.error(err))
The next step would be to use async/await
(≥ Node 7) or generators
with co
(≥ Node 4) to achieve synchronous like control flows for your asynchronous code.
const request = require('request-promise-native')
function getExtractFromWikipedia (title) {
return request({
uri: 'https://en.wikipedia.org/w/api.php',
qs: {
titles: title,
action: 'query',
format: 'json',
prop: 'extracts',
exintro: true,
explaintext: true
},
method: 'GET',
json: true
})
.then((body) => Object.keys(body.query.pages).map((key) => body.query.pages[key].extract))
.then((extracts) => extracts[0])
.catch((err) => {
console.error('getExtractFromWikipedia() error:', err)
throw err
})
}
// PREFER
async function getExtractFromWikipedia (title) {
let body
try {
body = await request({ /* same parameters as above */ })
} catch (err) {
console.error('getExtractFromWikipedia() error:', err)
throw err
}
const extracts = Object.keys(body.query.pages).map((key) => body.query.pages[key].extract)
return extracts[0]
}
// or
const co = require('co')
const getExtractFromWikipedia = co.wrap(function * (title) {
let body
try {
body = yield request({ /* same parameters as above */ })
} catch (err) {
console.error('getExtractFromWikipedia() error:', err)
throw err
}
const extracts = Object.keys(body.query.pages).map((key) => body.query.pages[key].extract)
return extracts[0]
})
getExtractFromWikipedia('Robert Cecil Martin')
.then((robert) => console.log(robert))
How should I write performant code?
In the first place, you should write clean code, then use profiling to find performance bottlenecks.
Never try to write performant and smart code first, instead, optimize the code when you need to and refer to true impact instead of micro-benchmarks.