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 508 of 534
What is Modulus Operator (%) in JavaScript?
The modulus operator (%) in JavaScript returns the remainder after dividing one number by another. It's also called the remainder operator and is commonly used in programming for tasks like checking if a number is even or odd. Syntax operand1 % operand2 Where operand1 is the dividend and operand2 is the divisor. The operation returns the remainder of the division. Basic Examples console.log(10 % 3); // 1 (10 ÷ 3 = 3 remainder 1) console.log(15 % 4); // 3 (15 ÷ 4 = 3 remainder 3) console.log(20 % 5); ...
Read MoreHow to redirect website after certain amount of time without JavaScript?
To redirect from an HTML page without JavaScript, use the META tag with the http-equiv="refresh" attribute. This provides an HTTP header that tells the browser to automatically redirect after a specified number of seconds. Through this method, you can automatically redirect your visitors to a new homepage or any other page. Set the content attribute to 0 if you want the redirect to happen immediately. Syntax Parameters The content attribute contains two parts separated by a semicolon: seconds - Number of seconds to wait before redirecting url - The destination ...
Read MoreHow to use JavaScript to redirect a webpage after 5 seconds?
In this tutorial, we learn to use JavaScript to redirect a webpage after 5 seconds. To redirect a webpage after a delay, we use the setTimeout() method combined with window.location.href to set the destination URL. JavaScript provides two timing methods for delayed execution: setTimeout() for single execution and setInterval() for repeated execution. For page redirection, setTimeout() is the preferred method. To redirect a page we use the window.location.href property to change the current page URL: window.location.href = "https://example.com"; Using setTimeout() Method (Recommended) The setTimeout() method executes a function once after a specified delay. ...
Read MoreWhat is unsigned Right Shift Operator (>>>) in JavaScript?
The unsigned right shift operator (>>>) shifts the binary representation of a number to the right by a specified number of positions, filling the leftmost bits with zeros regardless of the original sign bit. Syntax result = number >>> positions How It Works Unlike the signed right shift (>>) which preserves the sign bit, the unsigned right shift always fills with zeros from the left. This treats the number as an unsigned 32-bit integer. Example with Positive Number var a = 14; var b = 2; // Shift ...
Read MoreWhat is the difference between == and === in JavaScript?
In JavaScript, == (double equals) performs loose equality comparison with type coercion, while === (triple equals) performs strict equality comparison without type conversion. Double Equals (==) - Loose Equality The == operator converts operands to the same type before comparing them. This can lead to unexpected results: console.log(4 == 4); // true console.log('4' == 4); // true - string converted to number console.log(4 == '4'); // true - number converted to string console.log(0 == false); // ...
Read MoreHow to load a JavaScript function using the variable name?
In JavaScript, there are several ways to load and execute functions using variable names. This technique is useful when you need to dynamically call functions or store function references in variables. Functions are blocks of reusable code that can be stored in variables and called later. JavaScript supports both named functions and anonymous functions, giving you flexibility in how you organize and execute your code. Anonymous Functions Anonymous functions are functions without a name. They must be assigned to a variable to be callable. This is the primary way to load a JavaScript function using a variable ...
Read MoreHow to use JavaScript to hide a DIV when the user clicks outside of it?
To hide a DIV when the user clicks outside of it, you can listen for click events on the document and check if the clicked element is the target DIV or one of its children. This technique is commonly used for closing modals, dropdowns, and tooltips. Basic Implementation The simplest approach is to attach a click listener to the document and hide the DIV when the click target is not the DIV itself: #hideMe { ...
Read MoreWhat is the difference between decodeURIComponent and decodeURI?
JavaScript provides two methods for decoding URL-encoded strings: decodeURIComponent() and decodeURI(). Understanding their differences is crucial for proper URL handling in web applications. decodeURIComponent The decodeURIComponent() method decodes all encoded characters in a URI component, including reserved characters like =, &, and ?. Syntax decodeURIComponent(encodedURIComponent) Example Test decodeURIComponent function displayComponent() { ...
Read MoreWhat is the difference between single and double quotes in JavaScript?
In JavaScript, you can use either single quotes or double quotes to define strings. Both are functionally identical, but it's important to maintain consistency throughout your code. Syntax let singleQuoted = 'Hello World'; let doubleQuoted = "Hello World"; Basic Examples let message1 = "Hello, JavaScript!"; let message2 = 'Hello, JavaScript!'; console.log(message1); console.log(message2); console.log(message1 === message2); // Both are identical Hello, JavaScript! Hello, JavaScript! true Escaping Quotes When your string contains quotes, you need to escape them or use the opposite quote type: // Escaping ...
Read MoreHow to write a JavaScript function to get the difference between two numbers?
Use Math.abs() inside a JavaScript function to get the difference between two numbers in JavaScript. The Math.abs() method returns the absolute value, ensuring you always get a positive difference regardless of which number is larger. Syntax function getDifference(num1, num2) { return Math.abs(num1 - num2); } Example: Using Math.abs() for Difference You can try to run the following code to get the difference of numbers: var num1, num2; ...
Read More