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 406 of 801
How to get the first n values of an array in JavaScript?
In JavaScript, getting the first n elements from an array is a common operation. There are several built-in methods to accomplish this efficiently. First n values means extracting elements from index 0 to index n-1. For example, if n=3, you get elements at positions 0, 1, and 2. Using the slice() Method (Recommended) The slice() method is the most common and efficient way to get the first n elements. It creates a new array without modifying the original. Syntax array.slice(0, n) Example 1: Basic Usage ...
Read MoreHow to invert an object in JavaScript?
Inverting an object in JavaScript means swapping its keys and values, so that {name: "John", age: 25} becomes {"John": "name", "25": "age"}. This is useful for creating lookup tables or reverse mappings. There are several ways to invert objects in JavaScript, from using libraries like Underscore.js to native JavaScript methods. Using Underscore.js _.invert() The _.invert() function from Underscore.js provides a simple way to swap object keys and values. Syntax _.invert(object) Parameters: object − The object to invert Return Value: A new object with keys and values ...
Read MoreWhat is the relation between 'null' and '0' in JavaScript?
In JavaScript, the relationship between null and 0 creates a seemingly contradictory behavior that confuses many developers. While null is neither greater than nor equal to 0 in equality checks, it evaluates as greater than or equal to 0 in comparison operations. Understanding null and 0 null is a primitive value representing an intentional absence of value, while 0 is a number. The confusing behavior occurs due to JavaScript's type coercion rules, which differ between equality (==) and comparison (>, =,
Read MoreHow to remove the last digit of a number and execute the remaining digits in JavaScript?
In JavaScript, you can remove the last digit from a number using different approaches. The most common methods involve string manipulation or mathematical operations like bitwise operations. When working with numbers, you typically need to convert the number to a string to use string methods, or use mathematical operations to avoid type conversion altogether. Method 1: Using String substring() The substring() method extracts characters from a string between two specified indices. Syntax str.substring(startIndex, endIndex) To remove the last digit, use str.substring(0, str.length - 1) where the first parameter is the starting index ...
Read MoreWhat is the importance of '+' operator in type conversion in JavaScript?
JavaScript is a dynamically typed language where operations often automatically convert values to the appropriate type. The + operator plays a crucial role in type conversion, behaving differently depending on the operand types. JavaScript provides many ways to convert data from one form to another, with two most common types of conversion: Converting values to strings Converting values to numbers The '+' Operator's Dual Behavior The + operator in JavaScript serves two purposes: Addition: When both operands are numbers String ...
Read MoreHow to find the inner height and inner width of a browser window in JavaScript?
In this article we are going to learn how to find the inner height and inner width of a browser window using JavaScript. JavaScript defines window object methods and properties. The properties include inner height and inner width, which helps us to calculate the height and width of a window's content area excluding scrollbars, toolbars, and other browser UI elements. There are two main approaches to calculate the inner height and inner width of a window: Using innerHeight and innerWidth properties − The window.innerHeight and window.innerWidth properties return the viewport dimensions including padding ...
Read MoreHow to know the browser language and browser platform in JavaScript?
In this article we are going to discuss how to know the browser language and browser platform with the help of examples in JavaScript To know the various properties of the browser, JavaScript provides the Navigator object. The Navigator object has several properties, which include - connection, credentials, cookies, geolocation, language, platform, etc. We are concerned with Navigator.language and Navigator.platform to know the language and platform of the browser. Let us discuss about them in detail. Navigator.language The read-only Navigator.language property returns a string that represents the browser UI language. It returns a string that contains the ...
Read MoreHow to get a particular anchor in a document in JavaScript?
In this article we will learn how to get a particular anchor in a document in JavaScript. JavaScript provides multiple ways to access anchor elements ( tags) in a document. Anchor elements follow an array-like structure, allowing you to select specific anchors by index or use DOM methods to retrieve them. There are two primary methods to get a particular anchor element: using document.anchors collection or document.getElementsByTagName("a") method. Syntax The syntax to get a particular anchor tag is shown below: // Method 1: Using document.anchors collection document.anchors[index].innerHTML // Method 2: Using getElementsByTagName document.getElementsByTagName("a")[index].innerHTML ...
Read MoreHow to find the number of links in a document in JavaScript?
The document.links property in JavaScript provides access to all links in a document. It returns a collection of and elements that contain an href attribute. Syntax To get the total number of links in a document, use: document.links.length The document.links property returns an HTMLCollection that behaves like an array, allowing you to access individual links by index and get the total count using the length property. Example 1: Basic Link Count Here's a simple example to count the links in a document: JavaScript ...
Read MoreHow to display the domain of the server that loaded a document in JavaScript?
In this following article we are going to learn how to display the domain of the server that loaded a document in JavaScript. To display the domain of the server, we use domain property of the document interface, which returns the domain part of the current document. The domain property will return NULL if the document was created in memory. Attempting to set the domain property of the document interface results in SecurityError. To understand better, let's look into the syntax and usage of domain property with suitable examples in JavaScript. Using document.domain Property We can ...
Read More