Top 10 JavaScript’s Features With Code Examples
JavaScript, often hailed as the “language of the web,” has evolved from a simple scripting language to a powerful tool driving modern web development. With its versatility and ubiquity, JavaScript has become indispensable in crafting dynamic and interactive web experiences.
Here will see top 10 JavaScript’s features with code examples that make JavaScript a cornerstone of web development.
1. JavaScript’s Dynamic Typing:
JavaScript’s dynamic typing allows variables to hold values of any data type without explicit declaration.
This flexibility simplifies coding by enabling developers to write more concise and expressive code.However, it also requires careful attention to data types to prevent unexpected behavior.
let variable = 10; // variable is initially a number
console.log(typeof variable); // Output: number
variable = "Hello"; // Now variable is a string
console.log(typeof variable); // Output: string
variable = true; // Now variable is a boolean
console.log(typeof variable); // Output: boolean
2. Functions as First-Class Citizens:
In JavaScript, functions are treated as first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned as values from functions.
// Assigning a function to a variable
const greet = function (name) {
return "Hello, " + name + "!";
};
// Passing a function as an argument
function saySomething(func) {
return func("John");
}
console.log(saySomething(greet)); // Output: Hello, John!
// Returning a function from another function
function createMultiplier(multiplier) {
return function (number) {
return number * multiplier;
};
}
const double = createMultiplier(2);
console.log(double(5)); // Output: 10
This functional paradigm empowers developers to write more modular and reusable code, facilitating the implementation of advanced programming patterns such as functional programming and callbacks.
3. Prototypal Inheritance:
Unlike classical object-oriented languages, JavaScript utilizes prototypal inheritance, where objects inherit properties and methods directly from other objects.
This lightweight and flexible inheritance model fosters code reusability and allows for dynamic object behavior modification at runtime.
// Parent constructor function
function Animal(name) {
this.name = name;
}
// Adding a method to the prototype of Animal
Animal.prototype.sayName = function () {
console.log("My name is " + this.name);
};
// Child constructor function
function Dog(name, breed) {
// Call the parent constructor with 'this' context
Animal.call(this, name);
this.breed = breed;
}
// Set up inheritance: Dog inherits from Animal
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
// Adding a method specific to Dog
Dog.prototype.bark = function () {
console.log("Woof!");
};
// Creating instances
const animal = new Animal("Bob");
const dog = new Dog("Buddy", "Golden Retriever");
// Using inherited methods
animal.sayName(); // Output: My name is Bob
dog.sayName(); // Output: My name is Buddy
// Using child-specific method
dog.bark(); // Output: Woof!
4. Asynchronous Programming with Promises and Async/Await:
JavaScript’s asynchronous nature is pivotal in handling tasks such as fetching data from servers, processing user input, and performing time-consuming operations without blocking the main thread.
Promises and the async/await
syntax provide elegant solutions for managing asynchronous code, enhancing readability and maintainability.
// Function returning a Promise
function getData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data fetched successfully!");
}, 2000);
});
}
// Using the Promise
getData()
.then((result) => {
console.log(result); // Output after 2 seconds: Data fetched successfully!
})
.catch((error) => {
console.error("Error:", error);
});
// Async function using Await
async function fetchData() {
try {
const result = await getData();
console.log(result); // Output after 2 seconds: Data fetched successfully!
} catch (error) {
console.error("Error:", error);
}
}
// Calling the async function
fetchData();
5. JavaScript Closures:
Closures enable JavaScript functions to retain access to their lexical scope even after the parent function has finished executing.
This powerful feature is instrumental in creating private variables, implementing encapsulation, and facilitating modular code design.
function outerFunction() {
let outerVariable = "I am outer!";
function innerFunction() {
console.log(outerVariable); // Inner function accessing outerVariable
}
// Returning innerFunction, capturing the environment where it's defined
return innerFunction;
}
const myFunction = outerFunction(); // Assigning the returned inner function
// Executing the inner function
myFunction(); // Output: I am outer!
In this example, innerFunction
is defined inside outerFunction
. Even after outerFunction
finishes executing, innerFunction
still has access to the outerVariable
declared in its outer scope. This is possible due to closures, which allow inner functions to access variables from their outer scopes even after the outer function has completed execution.
6. Arrow Functions:
Introduced in ECMAScript 6 (ES6), arrow functions offer a concise syntax for defining anonymous functions, with implicit return values and lexical scoping of the this
keyword.
Arrow functions enhance code readability and reduce verbosity, especially in functional programming paradigms.
// Regular function expression
const add = function (x, y) {
return x + y;
};
console.log(add(3, 5)); // Output: 8
// Arrow function expression
const subtract = (x, y) => {
return x - y;
};
console.log(subtract(8, 3)); // Output: 5
// Arrow function with implicit return
const multiply = (x, y) => x * y;
console.log(multiply(4, 2)); // Output: 8
// Arrow function with single parameter
const square = (x) => x * x;
console.log(square(3)); // Output: 9
// Arrow function with no parameters
const greet = () => {
return "Hello!";
};
console.log(greet()); // Output: Hello!
7. Template Literals:
Template literals, denoted by backticks (`), allow for the interpolation of variables and expressions within strings, along with multiline string support.
This feature simplifies string manipulation and enhances code readability by eliminating the need for cumbersome concatenation or escaping characters.
const name = "Alice";
const age = 30;
// Using template literals to concatenate strings and variables
const message = `Hello, my name is ${name} and I am ${age} years old.`;
console.log(message); // Output: Hello, my name is Alice and I am 30 years old.
// Template literals can span multiple lines
const multiLineMessage = `
Hello ${name},
Welcome to our website!`;
console.log(multiLineMessage);
/* Output:
Hello Alice,
Welcome to our website!
*/
8. Modules in JavaScript
ES6 modules provide a standardized mechanism for organizing and encapsulating JavaScript code into reusable units.
By allowing for the creation of separate modules with explicit dependencies, JavaScript applications benefit from improved maintainability, scalability, and code organization.
In the following example:
module.js
defines and exports variables (greeting), functions (greet), and a class (Person) using the export keyword.index.js
imports the exported variables, functions, and classes using the import keyword. It then uses them in its own code.
// module.js
// Exporting variables and functions
export const greeting = "Hello";
export function greet(name) {
return `${greeting}, ${name}!`;
}
// Exporting a class
export class Person {
constructor(name) {
this.name = name;
}
greet() {
return `${greeting}, ${this.name}!`;
}
}
// index.js
// Importing variables and functions
import { greeting, greet } from "./module.js";
console.log(greet("Alice")); // Output: Hello, Alice!
// Importing a class
import { Person } from "./module.js";
const person = new Person("Bob");
console.log(person.greet()); // Output: Hello, Bob!
9. Destructuring Assignment:
Destructuring assignment enables efficient extraction of values from arrays and objects using a concise syntax. This feature simplifies variable assignment and function parameter handling, streamlining code and enhancing readability.
// Array destructuring
const numbers = [1, 2, 3, 4, 5];
const [first, second, ...rest] = numbers;
console.log(first); // Output: 1
console.log(second); // Output: 2
console.log(rest); // Output: [3, 4, 5]
// Object destructuring
const person = {
name: "Alice",
age: 30,
city: "New York",
};
const { name, age, city } = person;
console.log(name); // Output: Alice
console.log(age); // Output: 30
console.log(city); // Output: New York
10. Enhanced Object Literals in ES2015:
ES6 introduced enhancements to object literals, including shorthand property and method definitions, computed property names, and method definition shorthand.
These enhancements contribute to cleaner and more expressive object-oriented code, fostering improved code organization and maintainability.
// Basic object literal
const name = "Alice";
const age = 30;
const person = {
name: name,
age: age,
greet: function () {
return "Hello, " + this.name + "!";
},
};
console.log(person.greet()); // Output: Hello, Alice!
// Enhanced object literal
const enhancedPerson = {
name,
age,
greet() {
return `Hello, ${this.name}!`;
},
};
console.log(enhancedPerson.greet()); // Output: Hello, Alice!