You’ve probably heard it a lot: Work smart and not hard.
That advice couldn’t be more legit, especially when it comes to programming. Especially when it comes to Javascript.
Beyond just embracing the DRY (Do Not Repeat Yourself) concept, writing elegant and simple codes makes it easier for a codebase to be easily read and understood.
Here are 7 shortcuts you should use in your next Javascript project:
Template literals allow for multi-line and embedded string expressions. This can be especially useful when you want to concatenate 2 or more strings together, helping you to avoid the concatenation hell.
let fname, lname;
fname = "Lawson";
lname = "Luke";
console.log("Hello "+fname " "+ lname); console.log(`Hello ${fname} ${lname}`); Loops helps you to iterate through an array.
let foods = ["rice", "beans", "egg", "bread", "meat"];
for(let i =0; i<foods.length; i++){
console.warn(foods[i]);
} for(item of foods){
console.warn(item);
} Most times when a task is being executed, it can be in the process call other functions. But a recursive function is one which calls itself.
Let’s write a countdown function, one that counts down from a given number.
function countDown(num){
for(let i=num; i>0; i--){
console.log(i);
}
countDown(20); function countDown(num){
if(num === 0){
return;
}
console.log(num);
counter(num - 1);
}
countDown(20); Ternary operators allow for conditional rendering and can be executed with just one line of code.
const x = 50;
let answer;
if (x > 50) {
answer = 'greater than 50';
} else {
answer = 'less than 50';
} const answer = x > 50 ? 'greater than 50' : 'less than 50'; Let’s assume we want to print out all multiples of 3 from a given array.
We could loop through the array and check for the required condition like so:
let num = [1,2,3,4,15,18,27,36];
for(let i = 0; i<num.length; i++){
if(num[i]%3 === 0){
console.log(num[i]);
}
} function getNum(val){
return val%3 === 0
}
let result = num.filter(getNum);
console.log(result); Sometimes when assigning a variable value to another variable, it is good to ensure that the source variable is not null, undefined or empty.
if (variable1 !== null || variable1 !== undefined || variable1 !== '') {
let variable2 = variable1;
} const variable2 = variable1 || 'new'; With objects literals we can assign properties to objects.
const obj = { x:x, y:y }; const obj = { x, y }; Smart codes adds both elegance and readability to your codebase. This means your code can be easily understood and debugged afterwards.
Latest tech news and coding tips.
The Geolocation API allows a web application to access a user’s geographical location (latitude, longitude, and more), with…
1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…
In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…
Below is a comprehensive, beginner-friendly, yet deeply detailed guide to Boolean Algebra, complete with definitions, laws,…
Debugging your own code is hard enough — debugging someone else’s code is a whole…
Git is a free, open-source distributed version control system created by Linus Torvalds.It helps developers: Learn how to…