Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 411 of 801
Is there any way I can call the validate() function outside the initValidation() function in JavaScript?
When you define a function inside another function in JavaScript, it's only accessible within the parent function's scope. To call the validate() function outside of initValidation(), you have several approaches. The Problem In the following code, validate() is scoped inside initValidation() and cannot be accessed externally: function initValidation(){ // irrelevant code here function validate(_block){ // code here } } Method 1: Using Constructor Pattern Convert the parent function into a constructor and assign ...
Read MoreJoin every element of an array with a specific character using for loop in JavaScript
In JavaScript, you can join array elements with a specific character using a for loop. This approach gives you fine control over how elements are combined and what separators or wrappers to use. Problem Statement We need to create a function that takes an array and a string, then returns a new string where each array element is wrapped by the given string. For example: applyText([1, 2, 3, 4], 'a') should return 'a1a2a3a4a' Using for Loop Approach Here's how to solve this using a for loop to iterate through the array: ...
Read MoreReplace() with Split() in JavaScript to append 0 if number after comma is a single digit
When working with decimal numbers as strings, you often need to format them properly. This article shows how to append a zero to single-digit numbers after a comma using JavaScript's split() and replace() methods. Problem Statement Given a string like "250, 5", we need to: Append a zero if the number after the comma is a single digit Return -1 if the string contains more than one comma Solution Using split() and replace() We can combine split() and replace() methods to achieve this: const a = "250, 5"; const roundString = (str) ...
Read MoreGet filename from string path in JavaScript?
We need to write a function that takes in a string file path and returns the filename. Filename usually lives right at the very end of any path, although we can solve this problem using regex but there exists a simpler one-line solution to it using the string split() method of JavaScript and we will use the same here. Let's say our file path is − "/app/base/controllers/filename.js" Using split() Method Following is the code to get file name from string path − const filePath = "/app/base/controllers/filename.js"; const extractFilename = (path) => { ...
Read MoreCombine objects and delete a property with JavaScript
We have the following array of objects that contains two objects and we are required to combine both objects into one and get rid of the chk property altogether: const err = [ { "chk" : true, "name": "test" }, { "chk" : true, "post": "test" } ]; ...
Read MoreHow to create a function which returns only even numbers in JavaScript array?
Here, we need to write a function that takes one argument, which is an array of numbers, and returns an array that contains only the numbers from the input array that are even. So, let's name the function as returnEvenArray, the code for the function will be − Example const arr = [3, 5, 6, 7, 8, 4, 2, 1, 66, 77]; const returnEvenArray = (arr) => { return arr.filter(el => { return el % 2 === 0; }) }; ...
Read MoreStrange syntax, what does `?.` mean in JavaScript?
The ?. operator is called optional chaining and provides a safe way to access nested object properties without throwing errors when intermediate properties are undefined or null. The Problem with Regular Property Access Consider this nested object describing a person: const being = { human: { male: { age: 23 } } }; console.log(being.human.male.age); 23 This works fine, but what happens if we try to access a property that doesn't exist? ...
Read MoreWhat is this problem in JavaScript's selfexecuting anonymous function?
Let's analyze this JavaScript code snippet that demonstrates a common confusion with variable hoisting and function declarations in self-executing anonymous functions. var name = 'Zakir'; (() => { name = 'Rahul'; return; console.log(name); function name(){ let lastName = 'Singh'; } })(); console.log(name); Naive Analysis (Incorrect) At first glance, you might expect this sequence: Global variable name is set to 'Zakir' Inside the IIFE, name is ...
Read MoreHow to print star pattern in JavaScript in a very simple manner?
Here is a simple star pattern that we are required to print inside the JavaScript console. Note that it has to be printed inside the console and not in the output or HTML window: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Here's the code for doing so in JavaScript: Example const star = "* "; // where length is no of stars in longest streak const length = 6; for(let i = 1; i
Read MoreHow to deserialize a JSON into Javascript object?
Serialization is the process of converting an object such that it is transferable over the network. In JavaScript usually we serialize an Object into the JSON (JavaScript Object Notation) format. To deserialize a JSON string back into a JavaScript object, we use the JSON.parse() method. This method takes a JSON string and converts it into a JavaScript object or array that we can work with in our code. JavaScript object notation is commonly used to exchange data with web servers and REST APIs. The data we receive from a web server is always a string. To use this ...
Read More