Posts

Function Expressions in JavaScript

What are Function-Expressions? A Function-Expression is a function that is assigned to a variable or a property of an object. It can be named or anonymous. It is a function that is created at runtime and can be passed as an argument to another function. //example of a named Function-Expression let square = function ( num ) { return num * num ; } ; //example of an anonymous Function-Expression let add = function ( a , b ) { return a + b ; } ; How are Function-Expressions used? Function-Expressions are commonly used for following purposes: 1. To create and assign a function to a variable or property of an object. 2. To create a closure, i.e., functions within functions that can access the outer functions' variables. 3. To pass a function as an argument to another function. 4. To return a function from another function. //example of a Function-Expression used to create a closure function outerFunction ( x ) { function innerFunction ( y ) { re...

Functions in JavaScript

What is Function? Functions are an important concept in JavaScript, allowing you to wrap reusable blocks of code. They are a way to group together a set of statements to perform a specific task or return a value. They allow you to call that code whenever you need it, using a single command instead of typing out the same code multiple times. As a result your codebase become more organized, readable and maintainable. Function Declaration To define a named function in JavaScript, you use the 'function' keyword followed by the name of the function, a list of parameters enclosed in parentheses, and the JavaScript statements that define the function enclosed in curly brackets. This type of function can be called before the declaration in the code because JavaScript hoists function declarations to the top of their containing scope. Here is the basic syntax: // Function Declaration function functionName ( parameter1 , parameter2 , ... ) { // Function body // Code to be exe...

Method chaining in string

What is Method chaining in string? Method chaining is a technique in JavaScript where multiple methods are chained together in a single statement to perform consecutive operations on a string. This allows for cleaner and more concise code by avoiding the need for intermediate variables or repeated function calls. For example, instead of writing separate lines of code to perform different string operations, you can chain the methods together to achieve the same result in a single line. Example 1: let myString = "Hello, World!" ;   // Instead of let uppercaseString = myString. toUpperCase ( ) ; let reversedString = uppercaseString. split ( "" ) . reverse ( ) . join ( "" ) ; console. log ( reversedString ) ; // Output: "!DLROW ,OLLEH"   // Method chaining let newString = myString. toUpperCase ( ) . split ( "" ) . reverse ( ) . join ( "" ) ; console. log ( newString ) ; // Output: "!DLROW ,OLLEH" In the abov...

Strings are Immutable in JS

The immutability of strings in JavaScript In JavaScript, strings are immutable, which means that once they are created, they cannot be changed. This means that any operation on a string will always return a new string rather than modifying the original string itself. Example 1: let str = "Hello" ; let newStr = str. concat ( " World" ) ; console. log ( str ) ; // Output: "Hello" console. log ( newStr ) ; // Output: "Hello World" In the example above, the `concat()` method is used to concatenate the string " World" to the original string "Hello". However, the `concat()` method does not modify the original string, it returns a new string that contains the concatenated values. Points to note: 1. Strings are immutable in JavaScript, meaning they cannot be changed after they are created. 2. Operations on strings always create new strings. 3. Methods like concat(), slice(), trim(), etc., that appear to modify strings actu...

Array reduce() Method

Introduction The reduce() method in JavaScript is used to reduce an array to a single value by executing a callback function on each element of the array. The callback function takes in four parameters: the accumulator, the current value, the current index, and the array itself. The reduce() method iterates over the array from left to right and accumulates the values returned by the callback function. Syntax The syntax of the `reduce()` method is as follows: array. reduce ( callback ( accumulator , currentValue , currentIndex , array ) , initialValue ) Parameters: callback:  The function to execute on each element of the array. It takes in the accumulator, the current value, the current index, and the array itself. accumulator:  It accumulates the return values of the callback function. currentValue:  The current element being passed from the array. currentIndex (optional):  The index of the current element being processed. array (optional):  The array on wh...