Articles on Trending Technologies

Technical articles with clear explanations and examples

How to convert PHP array to JavaScript array?

Jennifer Nicholas
Jennifer Nicholas
Updated on 15-Mar-2026 3K+ Views

Converting PHP arrays to JavaScript arrays is essential when passing server-side data to client-side scripts. PHP's json_encode() function provides a reliable way to achieve this conversion for both single and multidimensional arrays. Syntax // PHP side $phpArray = array('value1', 'value2'); // JavaScript side var jsArray = ; Example: Converting Simple PHP Array Let's start with a simple PHP array containing user information: var arr = ; console.log(arr); // Output: ["Amit", "amit@example.com"] console.log(arr[0]); // Output: "Amit" ...

Read More

Prim's algorithm in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 2K+ Views

Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. It finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex. How Prim's Algorithm Works Let us look at an illustration of how Prim's algorithm works: 1. Choose any arbitrary node ...

Read More

How to toggle text with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Text toggling allows you to dynamically change displayed content based on user interaction. This is commonly used for showing/hiding information, switching between states, or creating interactive UI elements. Basic Toggle Example Here's a complete example that toggles between two text values when a button is clicked: body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; ...

Read More

How to count a depth level of nested JavaScript objects?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

We have an array of objects, which further have nested objects like this − const arr = [{ id: 0, children: [] }, { id: 1, children: [{ id: 2, children: [] }, { id: 3, children: [{ id: 4, children: [] }] }] }]; ...

Read More

How can I get seconds since epoch in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 441 Views

To get seconds since epoch in JavaScript, you can use Date.getTime() which returns milliseconds since January 1, 1970 (Unix epoch), then divide by 1000 to convert to seconds. Syntax var date = new Date(); var epochSeconds = Math.round(date.getTime() / 1000); Method 1: Using Math.round() The most common approach uses Math.round() to handle decimal precision: var currentDate = new Date(); var epochSeconds = Math.round(currentDate.getTime() / 1000); console.log("Seconds since epoch:", epochSeconds); console.log("Type:", typeof epochSeconds); Seconds since epoch: 1594821507 Type: number Method 2: Using Math.floor() For exact truncation without ...

Read More

If ([] == false) is true, why does ([] || true) result in []? - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 603 Views

If we look closely at the problem statement, the difference between ([] == false) and ([] || true) is the following − In the first case, we are using loose conditional checking, allowing type coercion to take over. While in the second case, we are evaluating [] to its respective Boolean (truthy or falsy) which makes use of the function Boolean() instead of type coercion under the hood. Let's now unveil the conversions that happen behind the scenes in both cases. Case 1 - ([] == false) According to the MDN docs, when two data ...

Read More

Validating email and password - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Suppose, we have this dummy array that contains the login info of two of many users of a social networking platform − const array = [{ email: 'usman@gmail.com', password: '123' }, { email: 'ali@gmail.com', password: '123' }]; We are required to write a JavaScript function that takes in an email string and a password string. The function should return a boolean based on the fact whether or not the user exists in the database. Example Following is the code − ...

Read More

With JavaScript how can I find the name of the web browser, with version?

Abhinaya
Abhinaya
Updated on 15-Mar-2026 391 Views

To find the name of the web browser with version in JavaScript, you can use the navigator object which provides information about the user's browser and system. Basic Browser Detection The traditional approach uses navigator.userAgent to detect browser types: Browser Detection Example var userAgent = navigator.userAgent; var opera = (userAgent.indexOf('Opera') != -1); var ie = (userAgent.indexOf('MSIE') != -1 || ...

Read More

Kruskal's algorithm in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 2K+ Views

Kruskal's algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a weighted, undirected graph. It works by systematically selecting the smallest weight edges while avoiding cycles. How Kruskal's Algorithm Works The algorithm follows these steps: Create a set of all edges in the graph Sort all edges by weight in ascending order While the set is not empty and not all vertices are covered: Remove the minimum weight edge from the set Check if this edge forms a cycle. If not, ...

Read More

How to toggle between adding and removing a class name from an element with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 300 Views

JavaScript provides several methods to toggle class names on HTML elements. The most efficient approach is using the classList.toggle() method, which automatically adds a class if it doesn't exist or removes it if it does. Using classList.toggle() Method The toggle() method is the simplest way to switch between adding and removing a class: .highlight { background-color: #3498db; ...

Read More
Showing 18361–18370 of 61,297 articles
Advertisements