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
Front End Technology Articles
Page 363 of 652
Add elements to a Dictionary in Javascript
In JavaScript, there are multiple ways to add elements to a dictionary-like structure. You can use plain objects, custom dictionary classes, or the ES6 Map object. Using Plain Objects The simplest approach is using JavaScript objects as dictionaries. You can add key-value pairs using bracket notation or dot notation: const dictionary = {}; // Using bracket notation dictionary["key1"] = "value1"; dictionary["key2"] = "value2"; // Using dot notation (for valid identifiers) dictionary.key3 = "value3"; console.log(dictionary); { key1: 'value1', key2: 'value2', key3: 'value3' } Custom Dictionary Class You can ...
Read MoreClearing a Dictionary using Javascript
In JavaScript, dictionaries can be implemented using plain objects or ES6 Maps. Both approaches provide methods to clear all key-value pairs from the container. Clearing Custom Dictionary Objects For custom dictionary implementations using plain objects, you can create a clear() method that resets the container: clear() { this.container = {} } Example with Custom Dictionary class MyMap { constructor() { this.container = {}; } ...
Read MoreLoop through a hash table using Javascript
Hash tables (also called hash maps) store key-value pairs and provide efficient lookup. To iterate through all entries, we need to traverse each chain in the underlying storage container since hash tables typically use chaining to handle collisions. Creating a forEach Method The forEach method loops over all key-value pairs in the hash table and executes a callback function for each pair. We iterate through each chain in the container and process every key-value pair. forEach(callback) { // For each chain in the hash table this.container.forEach(elem => { ...
Read MoreHow to convert a string in to a function in JavaScript?
To convert a string into a function in JavaScript, the eval() method can be used. This method takes a string as a parameter and evaluates it as JavaScript code, which can include function definitions. Syntax eval(string); Example: Converting String to Function In the following example, a string contains a JSON object where the 'age' property holds a function as a string. Using eval(), we convert this string into an actual executable function. var string = '{"name":"Ram", "age":"function() {return 27;}", "city":"New jersey"}'; var fun ...
Read MoreCan re-declaring a variable destroy the value of that variable in JavaScript?
Re-declaring a variable will not destroy the value of that variable, until and unless it is assigned with some other new value. If we look at the following example variables "x" and "y" were assigned with values 4 and 8 respectively, later on when those variables were reassigned, the old values were replaced with the new values and displayed as shown in the output. Example: Re-declaring with New Values var x = new Number(4); ...
Read MoreWhat is the importance of _without() method in JavaScript?
The _.without() method is part of the Underscore.js library that creates a new array by excluding specified values from an original array. It performs case-sensitive matching and removes all occurrences of the specified values. Syntax _.without(array, *values) Parameters array - The source array to filter *values - One or more values to exclude from the array Return Value Returns a new array with the specified values removed, preserving the original array order. Example: Removing Numbers The following example removes multiple numeric values from an array: ...
Read MoreWhat is the importance of _.union() method in JavaScript?
The _.union() method from the Underscore.js library creates a new array containing unique values from multiple input arrays. It performs a union operation, removing duplicates while preserving the original order of first occurrence. Syntax _.union(array1, array2, ...arrayN); Parameters array1, array2, ...arrayN: Two or more arrays to be combined. The method accepts any number of arrays as arguments. Return Value Returns a new array containing all unique values from the input arrays, maintaining the order of first occurrence. Example with Numbers In the following example, _.union() combines multiple arrays and removes ...
Read MoreHow to get the application name and version information of a browser in JavaScript?
JavaScript provides a navigator object that contains information about the browser. To get the application name and version information, the navigator object provides navigator.appName and navigator.appVersion properties respectively. Application Name of the Browser The navigator.appName property returns the application name of the browser. However, due to historical reasons, most modern browsers (Chrome, Firefox, Safari, Edge) return "Netscape" regardless of their actual name. Example document.write("Application Name: " + navigator.appName); Output Application Name: Netscape Browser Version Information The navigator.appVersion property provides ...
Read MoreUsing a JavaScript object inside a static() method?
In JavaScript, static methods belong to the class itself, not to instances. You cannot directly access instance objects or use this inside static methods. However, you can pass objects as parameters to static methods to access their properties. Problem: Direct Object Access in Static Methods In the following example, trying to call a static method on an instance will result in an error because static methods should be called on the class, not the instance. class Company { constructor(branch) { ...
Read MoreGet the first and last item in an array using JavaScript?
JavaScript arrays are 0-indexed, meaning the first element is at position 0 and the last element is at position length - 1. Here are several ways to access the first and last elements of an array. Using Index Access The most straightforward method uses bracket notation with index positions: let arr = [1, 'test', {}, 'hello', 42]; // First element console.log("First element:", arr[0]); // Last element console.log("Last element:", arr[arr.length - 1]); First element: 1 Last element: 42 Using Array Methods JavaScript provides built-in methods for accessing ...
Read More