Modern JavaScript
JavaScript started as a simple scripting language primarily used to enhance web pages. However, as web applications became more complex and demanding, the need for more sophisticated language features in JavaScript has undergone significant transformations, especially with the introduction of ES6 (ECMAScript 2015) and other versions. These updates have brought new features and syntax enhancements that streamline development and improve code readability and maintainability. Through this article let’s dive into some new features introduced in modern JavaScript
Callback functions
A callback is a function passed as an argument to another function This technique allows a function to call another function. A callback function can run after another function has finished and it is often used with the asynchronous operations
// function to display the total value
const display = (s) => {
console.log(`Total = ${s}`)
}
// function to get the total with the callback function as an argument
const getTotal = (x, y, display) => {
let total = x + y
// call the callback function
display(total)
}
// call the getTotal function with a display callback function as an argument
getTotal(5, 3, display)
// Output
// Total = 8this above example demonstrates getting the total using a function called getTotal and displaying that output using a callback function called display
Async/Await
async and await make promises easier to write and async makes a function return a Promise await makes a function wait for a Promise
// function to fetch data from an API
const fetchData = async () => {
try {
//this will await until the fetching data is complete
let response = await fetch('https://jsonplaceholder.typicode.com/users');
let data = await response.json();
console.log(data);
} catch (error) {
console.log('Error fetching data:', error);
}
}
// call the function
fetchData();
//Output
/*[
{
id: 1,
name: 'Leanne Graham',
username: 'Bret',
email: 'Sincere@april.biz',
address: {
street: 'Kulas Light',
suite: 'Apt. 556',
city: 'Gwenborough',
zipcode: '92998-3874',
geo: [Object]
},
phone: '1-770-736-8031 x56442',
website: 'hildegard.org',
company: {
name: 'Romaguera-Crona',
catchPhrase: 'Multi-layered client-server neural-net',
bs: 'harness real-time e-markets'
}
},
{
id: 2,
name: 'Ervin Howell',
username: 'Antonette',
email: 'Shanna@melissa.tv',
address: {
street: 'Victor Plains',
suite: 'Suite 879',
city: 'Wisokyburgh',
zipcode: '90566-7771',
geo: [Object]
},
phone: '010-692-6593 x09125',
website: 'anastasia.net',
company: {
name: 'Deckow-Crist',
catchPhrase: 'Proactive didactic contingency',
bs: 'synergize scalable supply-chains'
}
},
]*/this shows async function waits until the data is fetched from the API and once it's done it will give us the response or if something wrong happens it will give us an error because of the use of a try-catch block as well.
Asynchronous
Functions running in parallel with other functions are called asynchronous
// function to get time
const getTime = () => {
let date = new Date()
console.log(date)
}
// function to call the getTime function again and again after 1 second
setInterval(getTime, 1000)
//Output
/*2024-10-18T02:40:05.354Z
2024-10-18T02:40:06.365Z
2024-10-18T02:40:07.373Z
2024-10-18T02:40:08.383Z
2024-10-18T02:40:09.388Z
2024-10-18T02:40:10.402Z
2024-10-18T02:40:11.413Z
*/
this above example demonstrates a simple clock which runs the getTime function and calls that function again and again after 1 second Parallelly with the getTime function
Promises
Producing code is code that can take some time Consuming code is code that must wait for the result A Promise is an Object that links Producing code and Consuming code
// Asynchronous operation
let myPromise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve("The operation was successful!");
} else {
reject("There was an error.");
}
});
myPromise.then( (message)=> {
console.log(message); // This runs if the promise is fulfilled
}).catch( (error) =>{
console.log(error); // This runs if the promise is rejected
});
// Output
// The operation was successful!this above example describes the asynchronous operation which is waiting to complete the operation and based on that operation results we can get the resolved or rejected output
Get Dushan Senadheera’s stories in your inbox
Join Medium for free to get updates from this writer.
The above sections describe a few important topics in the modern JavaScript programming language and it revolutionized the way we write code, making it more concise, readable, and maintainable as well.









