An Interest In:
Web News this Week
- March 14, 2024
- March 13, 2024
- March 12, 2024
- March 11, 2024
- March 10, 2024
- March 9, 2024
- March 8, 2024
May 7, 2020 04:48 am GMT
Original Link: https://dev.to/shoaib03/9-tricks-to-write-less-javascript-507i
9 Tricks To Write Less JavaScript.
Hi Dev , Thanks for opening my blog. I hope you are doing well and ready to learn some Tricks To Write Less JavaScript .
So, let's start!
1. Declaring Variables
//Longhandlet x;let y;let z = "post";//Shorthandlet x, y, z = "post";
2. Assignment Operator
//Longhandx = x + y;x = x - y;//Shorthandx += y;x -= y;
3. Ternary Operator
let answer, num = 15;//Longhandif (num > 10) { answer = "greater than 10";} else { answer = "less than 10";}//Shorthandconst answer = num > 10 ? "greater than 10" : "less than 10";
4. Short for Loop
const languages = ["html", "css", "js"];//Longhandfor (let i = 0; i < languages.length; i++) { const language = languages[i]; console.log(language);}//Shorthandfor (let language of languages) console.log(language);
5. Template Literals
const name = "Dev";const timeOfDay = "afternoon";//Longhandconst greeting = "Hello " + name + ", I wish you a good " + timeOfDay + "!";//Shorthandconst greeting = `Hello ${name}, I wish you a good ${timeOfDay}!`;
6. Arrow Function
//Longhandfunction sayHello(name) { console.log("Hello", name);}list.forEach(function (item) { console.log(item);});//ShorthandsayHello = name => console.log("Hello", name);list.forEach(item => console.log(item));
7. Object Array Notation
//Longhandlet arr = new Array();arr[0] = "html";arr[1] = "css";arr[2] = "js";//Shorthandlet arr = ["html", "css", "js"];
8. Object Destructuring
const post = { data: { id: 1, title: "9 trick to write less Javascript", text: "Hello World!", author: "Shoaib Sayyed", },};//Longhandconst id = post.data.id;const title = post.data.title;const text = post.data.text;const author = post.data.author;//Shorthandconst { id, title, text, author } = post.data;
9. Object with identical Keys and Values
//Longhandconst userDetails = { name: name, // 'name' key = 'name' variable email: email, age: age, location: location,};//Shorthandconst userDetails = { name, email, age, location };
That's It .
Thanks for reading! My name is Shoaib Sayyed; I love helping people to learn new skills . You can follow me, if youd like to be notified about new articles and resources.
Original Link: https://dev.to/shoaib03/9-tricks-to-write-less-javascript-507i
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To