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
Javascript Articles
Page 468 of 534
How to create a custom function similar to find() method in JavaScript?
Let's say we have the following records of studentId and studentName and want to check a specific student name: const studentDetails = [ { studentId: 101, studentName: "John" }, { studentId: 102, studentName: "David" }, { studentId: 103, ...
Read MoreCounting elements of an array using a recursive function in JS?
A recursive function calls itself until a base condition is met. In JavaScript, we can use recursion to count array elements by reducing the array size in each recursive call. How Recursive Counting Works The recursive approach works by: Base case: If array is empty, return 0 Recursive case: Return 1 + count of remaining elements Example function countNumberOfElementsUsingRecursive(listOfMarks) { if (listOfMarks.length == 0) { return 0; } return 1 + countNumberOfElementsUsingRecursive(listOfMarks.slice(1)); } ...
Read MoreDisplay whether the office is closed or open right now on the basis of current time with JavaScript ternary operator
Let's say we are matching the current date and time with the business hours. We need to display whether the office is closed or open right now on the basis of current time. We can get the current hour from the Date object and use the JavaScript ternary operator to determine if the office is open or closed based on business hours. Syntax const currentHour = new Date().getHours(); const status = (condition) ? 'Open' : 'Closed'; Example Office ...
Read MoreCreate new array without impacting values from old array in JavaScript?
In JavaScript, directly assigning an array to a new variable creates a reference to the original array, not a copy. This means changes to the new variable will affect the original array. To create a truly independent copy, you need to use specific cloning methods. The Problem with Direct Assignment When you assign an array directly, both variables point to the same array in memory: var originalArray = ["John", "Mike", "Sam", "Carol"]; var newArray = originalArray; // This creates a reference, not a copy newArray.push("David"); console.log("Original array:", originalArray); console.log("New array:", newArray); ...
Read MoreHow to display JavaScript array items one at a time in reverse on button click?
In this tutorial, we'll learn how to display JavaScript array items one at a time in reverse order when a button is clicked. This technique is useful for creating step-by-step displays or interactive presentations. The Array and Button Setup First, let's define our array of names: var listOfNames = [ 'John', 'David', 'Bob' ]; And create a button that will trigger the reverse display: Click The Button To get the Reverse Value How It Works The approach ...
Read MoreHTML form action and onsubmit validations with JavaScript?
HTML form validation is essential for ensuring data integrity before submission. The onsubmit event allows you to validate form data and prevent submission if validation fails. How Form Validation Works The onsubmit event handler must return true to allow submission or false to prevent it. When validation fails, the form submission is blocked. Basic Validation Example Form Validation Example Enter "gmail" to proceed: ...
Read MoreCheck whether a value exists in JSON object?
In JavaScript, you can check whether a value exists in a JSON object (or array of objects) using several approaches. Let's explore different methods to find values efficiently. Consider the following JSON object structure: var apiJSONObject = [ {subjectName: "MySQL"}, {subjectName: "Java"}, {subjectName: "JavaScript"}, {subjectName: "MongoDB"} ] Using for Loop (Traditional Approach) The basic approach uses a for loop to iterate through the array and check each object: var apiJSONObject = [ {subjectName: "MySQL"}, ...
Read MoreHow to use JavaScript map() method to access nested objects?
The map() method can be used to iterate through arrays of nested objects and safely access their properties using conditional checks. Understanding Nested Objects When working with arrays containing objects that have different structures, some objects may have nested properties while others don't. Here's our sample data: var details = [ { id: "101", firstName: "John", lastName: "Smith", age: 25, ...
Read MoreHow to convert comma separated text in div into separate lines with JavaScript?
Converting comma-separated text in a div into separate lines is a common requirement in web development. This can be achieved using JavaScript's string manipulation methods along with DOM manipulation. Let's say we have the following comma-separated text in a div: This, is, the, first, JavaScript, program To convert comma-separated text into separate lines, we need to use trim() along with split() based on the comma separator. Method 1: Converting to List Items This approach splits the comma-separated text and converts each item into a list element: ...
Read MoreCall a JavaScript class object with variable?
In JavaScript, you can call a class constructor with variables by passing the variable as an argument. There are several ways to achieve this, including direct instantiation and using eval(). Basic Class Instantiation with Variables The most straightforward approach is to pass variables directly to the class constructor: class FirstClass { constructor(message) { this.message = message; } } var message = 'This is the Class Demo'; var object = new FirstClass(message); console.log(object.message); This is the Class ...
Read More