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
Articles on Trending Technologies
Technical articles with clear explanations and examples
How many values does javascript have for nothing?
JavaScript has two primitive values that represent "nothing": null and undefined. Both indicate the absence of a meaningful value, but they have distinct purposes and behaviors. Undefined A variable is undefined when it has been declared but not assigned a value, or when a function doesn't explicitly return anything. JavaScript automatically assigns undefined in these cases. Example 1 - Variable Declaration When a variable is declared but not assigned a value: var str; console.log('The value of given variable is:', str); The value of given variable is: undefined Example 2 ...
Read MoreWhat is the most efficient way to deep clone an object in JavaScript?
In JavaScript, objects are reference types, meaning simple assignment creates a reference to the original object, not a copy. Deep cloning creates a completely independent copy with separate memory allocation for nested objects and arrays. Understanding Deep vs Shallow Cloning Shallow cloning copies only the top-level properties, while deep cloning recursively copies all nested objects. Methods like spread operator and Object.assign() only perform shallow copies. Using JSON.parse() and JSON.stringify() (Most Common) The most efficient and widely-used approach combines JSON.stringify() to convert the object to a string, then JSON.parse() to create a new object from that string. ...
Read MoreHow to duplicate Javascript object properties in another object?
In JavaScript, objects are collections of key-value pairs where properties are identified by string keys. There are several ways to duplicate object properties from one object to another, each with different characteristics for handling nested objects and references. Using Spread Operator (...) The spread operator creates a shallow copy of an object by spreading its enumerable properties into a new object. It's represented by three dots (...) and is the most modern and readable approach. Example 1 This example demonstrates how the spread operator copies objects in JavaScript: var employee = { ...
Read MoreWindow.onload vs onDocumentReady in javascript
In JavaScript, window.onload and $(document).ready() are two different methods for executing code when a page loads, each with distinct timing and behavior. Window.onload The window.onload method executes after the entire web page has completely loaded, including all DOM elements, stylesheets, images, and other external resources. It only allows one function assignment — subsequent assignments will overwrite previous ones. Syntax window.onload = function() { // Code executes after everything loads }; window.onload = (event) => { // Arrow function syntax }; Example: Window.onload Behavior ...
Read MoreHow to find operating system in the client machine using JavaScript?
The operating system of the client machine can be detected using JavaScript navigator properties. This is useful for providing platform-specific functionality or displaying system information to users. Using navigator.appVersion The navigator.appVersion property returns a string containing browser and operating system information. This method allows you to parse the version string to identify the OS. Syntax navigator.appVersion Example 1: Detecting Operating System This example demonstrates how to detect the client's operating system using navigator.appVersion: Click to get the operating system ...
Read MoreDisadvantages of using innerHTML in JavaScript
The innerHTML property in JavaScript allows you to get or set the HTML content inside an element. While convenient, it comes with several significant disadvantages that developers should consider when manipulating the DOM. What is innerHTML? The innerHTML property represents the HTML content inside an element. It allows you to read or modify the HTML markup within a selected DOM element. Original content let container ...
Read MoreHow to read and write a file using JavaScript?
File operations in JavaScript are handled through Node.js using the built-in fs (File System) module. This module provides methods to read from and write to files asynchronously. Writing Files with writeFile() The writeFile() method creates a new file or overwrites an existing file with the specified content. Syntax fs.writeFile(path, data, options, callback) Parameters path: File path or filename where data will be written data: Content to write to the file (string, buffer, or typed array) options: Optional encoding (default: 'utf8') or ...
Read MoreDifference between test () and exec () methods in Javascript
The RegExp.prototype.test() and RegExp.prototype.exec() methods are JavaScript methods used to handle regular expressions. These methods provide different ways to search for patterns in strings and return different types of results. What are regular expressions? Regular expressions are patterns used to search for character combinations in strings. In JavaScript, regular expressions are treated as objects and are commonly referred to as "regex" or "RegExp." The exec() Method The exec() method searches for a match in a string and returns an array containing the match details if found, or null if no match is found. The returned array ...
Read Morewhat is the significance of finally in javascript?
The finally block in JavaScript executes after the try and catch blocks, regardless of whether an error occurred or not. It is commonly used for cleanup tasks like closing connections, releasing resources, or resetting state. Syntax try { // Code that may throw an error } catch (error) { // Runs only if try throws an error } finally { // Always runs — after try, or after catch } Block Combinations try + catch + finally ...
Read MoreHow do we check if an object is an array in Javascript?
In JavaScript, typeof returns "object" for arrays, which makes it unreliable for array detection. There are three better approaches: Array.isArray(), the constructor property, and instanceof. The Problem with typeof typeof returns "object" for arrays, objects, and null − it cannot distinguish arrays from other objects ? let str = "Abdul Rawoof"; let arr = [1, 3, 5, 8]; let num = 90; let dict = {a: 1, b: 2}; console.log(typeof str); // "string" console.log(typeof arr); // "object" ← not "array" console.log(typeof num); // "number" console.log(typeof dict); // ...
Read More