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 499 of 534
Add a method to a JavaScript object constructor?
In JavaScript, adding a method (function) to a constructor is different from adding a method to a regular object. To add a method to a constructor, we need to define it inside the constructor or in its prototype (using which JavaScript objects inherit features from one another). We can create an object in the following ways in JavaScript: Using the function Constructor (older way): function person(){ } const p = new person(); Using ES6 Class Syntax (i.e., modern way): class person{ constructor(){} } const p = new person(); ...
Read MoreBinary Search program in JavaScript
The binary search algorithm works on the divide and conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, the array should be sorted. In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left sub-array, right sub-array, or return the middle element. In this article, we are given a sorted array of integers, and our task is to search for the given target element ...
Read MoreAdding default search text to search box in HTML with JavaScript?
A default search text is a text that provides suggestions or a hint of what to search for in the search (input) box. Following is an illustration of a search box, where you can observe a default search text "Search your favorite tutorials…": Search your favorite tutorials... The above default search text will suggest to visitors that they can search for their favorite tutorial, which they want to read or open on the website. HTML placeholder Attribute In HTML, the text that appears inside a ...
Read MoreAdd elements to a Queue using Javascript
In JavaScript, there is no built-in queue data structure like other programming languages. However, you can implement a queue using an array object and perform operations like push() to add elements at the end and shift() to remove the first element. The following diagram illustrates the queue data structure and its FIFO (First In, First Out) principle: 10 20 30 40 ...
Read MoreHow to set text font family in HTML?
To change the text font family in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML tag, with the CSS property font-family. HTML5 does not support the tag, so CSS styles are used to control font appearance. Keep in mind that the usage of the style attribute overrides any style set globally. It will override any style set in the HTML tag or external style sheet. Syntax Content here Example: Basic Font ...
Read MoreHow can I trigger a JavaScript click event?
In JavaScript, you can programmatically trigger a click event on an element using the click() method. This is useful for automating user interactions or creating custom behaviors. Using the click() Method The click()
Read MoreCan we make print button without JavaScript?
In this tutorial, we will learn to make a print button without writing separate JavaScript files. Most websites have print functionality that opens the browser's print dialog when clicked. While HTML doesn't have a built-in print method, we can use the window.print() method directly in HTML attributes. This allows us to create print functionality without external JavaScript files. Syntax The basic syntax for invoking the print method: window.print(); // Opens browser's print dialog Using the onClick Event We can call window.print() directly in HTML using the onClick event attribute. Syntax ...
Read MoreHow to find out which JavaScript events fired?
In this tutorial, we will learn how we can find out which JavaScript event is fired or triggered. We can add events to HTML elements to perform some actions on the webpage like changing text, form submission, etc. There are two ways of finding which JavaScript event is fired − Using Developer Tools Using Inspect Element Let us discuss both of them one by one with coding examples. Using Developer Tools We can find out which JavaScript event is fired by using developer tools in the browser. ...
Read MoreWhat is the difference between local storage vs cookies?
Local storage and cookies are two different ways to store data in web browsers, each with distinct characteristics and use cases. Understanding their differences helps you choose the right storage mechanism for your web application. Storage Capacity Local storage offers significantly more storage space than cookies: Local Storage: Typically 5-10 MB per domain Cookies: Limited to 4 KB per cookie, with a maximum of about 20 cookies per domain Server Communication The key difference lies in how they interact with servers: // Local Storage - stays on client side localStorage.setItem('username', 'john_doe'); ...
Read MoreHow to secure my JavaScript using "Strict mode"?
In this tutorial, we are going to learn how to secure my JavaScript using "Strict mode". There are some errors in the code which are just ignored by the JavaScript engine and if any line fails it performs nothing. To indicate that there is an error we can use strict mode which will make the JavaScript engine throw an error. The 'use strict' is a literal expression which is a directive we can add to the code. We can add this 'use strict' directive to the whole script, a function, or a class. Syntax Now let's see ...
Read More