Javascript Articles

Page 312 of 534

Write a number array and add only odd numbers?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 1K+ Views

In JavaScript, you can sum only the odd numbers from an array by using the modulus operator (%) to check if a number is odd, then adding it to a running total. How It Works The modulus operator returns the remainder when a number is divided by 2. If the remainder is not equal to 0, the number is odd. Example: Summing Odd Numbers var tot = 0; var a = [1, 45, 78, 9, 78, 40, 67, 76]; ...

Read More

How to get the number of the internet host port for the current page in JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 784 Views

In this tutorial, we will learn to get the number of internet host port for the current page in JavaScript. The Port number is a 16-bit unique identifier used by network protocols. Port numbers range from 0-65535 and help servers identify specific processes or services. Each port number has its own identity and purpose in network communication. Following are the methods by which we can get the port number of the internet host: Using the location.port Property Using the URL interface Using the location.port Property ...

Read More

How to return the id of the first image in a document with JavaScript?

Kumar Varma
Kumar Varma
Updated on 15-Mar-2026 588 Views

To return the id of the first image in a document, use the document.images property in JavaScript. This property returns a collection of all image elements in the document. Syntax document.images[0].id How It Works The document.images collection contains all elements in the document, indexed starting from 0. To get the first image's id, access document.images[0].id. Example Here's a complete example showing how to get the id of the first image: ...

Read More

How to add a number and a string in JavaScript?

vineeth.mariserla
vineeth.mariserla
Updated on 15-Mar-2026 11K+ Views

In JavaScript, when you use the + operator with a number and a string, JavaScript performs concatenation instead of mathematical addition. This is because the presence of a string changes the operation from addition to string concatenation. How It Works JavaScript follows these rules when using the + operator: Number + Number: Mathematical addition String + Number: String concatenation (number is converted to string) Number + String: String concatenation (number is converted to string) Example var a = 5 + 5; ...

Read More

How to apply a function simultaneously against two values of the array from left-to-right?

Manikanth Mani
Manikanth Mani
Updated on 15-Mar-2026 237 Views

The reduce() current) ? prev : current; }); document.write("Maximum value: " + max + ""); // Count occurrences var fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']; var count = fruits.reduce(function(acc, fruit) { ...

Read More

How to get the number of forms in a document with JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 1K+ Views

In this tutorial, we will learn how to get the number of forms in a document using JavaScript. Since different methods are used to create forms in an HTML document, it might get tricky to get the number of forms present. However, you can use some methods to count the forms created without the form tag. Using the length Property The most widely accepted method to create forms in HTML documents is using the tag. Some websites also use tags to create forms in the document. Hence, we will discuss the method to get the number ...

Read More

How to find the name of the first form in a document with JavaScript?

Abhishek
Abhishek
Updated on 15-Mar-2026 1K+ Views

In this tutorial, we will learn how to find the name of the first form in an HTML document. Sometimes, we use more than one form tags in the HTML document for different purposes. At that time, we can face difficulties in finding the specific element to add some more properties to it. In such conditions, we can use the forms property of the "document" interface to access all the elements present in the document individually as well as collectively. Document.forms Property As we discussed above, the forms property is used to access all the forms ...

Read More

How to find the number of anchors with JavaScript?

Abhishek
Abhishek
Updated on 15-Mar-2026 343 Views

In this tutorial, we will learn how to count the number of anchor elements ( tags) in an HTML document using JavaScript. The document.anchors property provides a way to access all anchors with a name attribute in the document. Note: The document.anchors property is deprecated and only counts anchors with a name attribute. For modern development, use document.querySelectorAll('a') instead. Using document.anchors (Deprecated) The document.anchors property returns an HTMLCollection containing all anchor elements that have a name attribute. You can use the length property to get the count. Syntax let anchors = document.anchors; let numberOfAnchors ...

Read More

How to add properties and methods to an object in JavaScript?

V Jyothi
V Jyothi
Updated on 15-Mar-2026 315 Views

In JavaScript, you can add properties and methods to objects using several approaches. The most common methods include direct assignment, using the prototype property for constructor functions, and using Object.defineProperty(). Method 1: Direct Property Assignment The simplest way to add properties to an existing object is direct assignment: Direct Property Assignment let car = { brand: "Toyota", ...

Read More

How to show name/value pairs of cookies in a document with JavaScript?

Abhishek Kumar
Abhishek Kumar
Updated on 15-Mar-2026 947 Views

We use the cookie property of the document object to show the name/value pairs of cookies in a document with JavaScript. The document object, part of the DOM, corresponds to the current web page that the browser has loaded. It contains all the information about the condition of the browser as well as the web page. A server serves requests when a connection is set up and forgets everything about the user as soon as the connection is closed. This posed a nasty problem of bad user experience to the community. A cookie resolves this problem by storing small ...

Read More
Showing 3111–3120 of 5,340 articles
« Prev 1 310 311 312 313 314 534 Next »
Advertisements