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
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to encode a string in JavaScript?
In JavaScript, string encoding converts text into different formats for various purposes like URL handling, Base64 encoding, or data transmission. JavaScript provides several built-in methods for encoding strings depending on your specific needs. Encoding is the process of converting data from one format to another. Unlike encryption, encoding doesn't require keys and is primarily used to ensure data remains usable across different systems and protocols. Using btoa() Method The btoa() method encodes a string into Base64 format, which is commonly used for data transmission and storage. Syntax btoa(string) Where string is the text ...
Read MoreJavaScript program to get a variable to count up/down on keyboard press.
When working with JavaScript, handling keyboard events and creating a program that gets a variable to count up/down on keyboard press can be an essential skill. Keyboard events such as keydown and keyup in JavaScript are actions that occur when a user interacts with the keyboard. This article explains how to create a JavaScript program that increments or decrements a variable based on specific keyboard key presses. This feature can be helpful in interactive websites, like games or tools that check user input. Table of Content You can create a JavaScript program to get a variable to count ...
Read MoreHow to add a character to the beginning of every word in a string in JavaScript?
We need to write a function that takes two strings and returns a new string with the second argument prepended to every word of the first string. For example: Input → 'hello stranger, how are you', '@@' Output → '@@hello @@stranger, @@how @@are @@you' If the second argument is not provided, we'll use '#' as the default character. Solution Using split() and map() The most straightforward approach is to split the string into words, prepend the character to each word, and join them back: const str = 'hello stranger, how are ...
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 MoreProgram to append two given strings such that, if the concatenation creates a double character then omit one of the characters - JavaScript
We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string. If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters. Let's say the following are our strings in JavaScript − Problem Example const str1 = 'Food'; const str2 = 'dog'; // Expected output: 'Foodog' (last 'd' of 'Food' matches first 'd' of 'dog') Solution Let's write the code for this function − ...
Read MoreHow to detect all active JavaScript event handlers?
JavaScript doesn't provide a built-in method to detect all active event handlers on a page. However, there are several approaches to identify event listeners attached to DOM elements. Using jQuery to Detect Event Handlers jQuery provides a convenient way to access event data for elements that have jQuery event handlers attached. Demo Text // Attach jQuery event handler ...
Read MoreWhat is onkeydown event in JavaScript?
The onkeydown event in JavaScript triggers when a user presses down a key on the keyboard. This event occurs before the key is released and before the character appears in the input field, making it useful for intercepting keystrokes and implementing custom keyboard behaviors. Syntax element.onkeydown = function(event) { // Handle keydown event }; // Or using addEventListener element.addEventListener('keydown', function(event) { // Handle keydown event }); Basic Example Onkeydown Example ...
Read MoreHow to style background image to no repeat with JavaScript DOM?
In this tutorial, we will learn to style the background image to no repeat with JavaScript DOM. To style the background image to no repeat with JavaScript, use the backgroundRepeat property. It allows you to set whether the background image repeats or not on a page. The use of images in the background really makes the page attractive. But before using the image in the background, one must completely understand the properties used to set the images as the background. Otherwise, it will create problems like not showing the full image in the background, repeating the image horizontally or ...
Read MoreNamed arguments in JavaScript.
Named arguments in JavaScript allow you to pass parameters to functions using an object, making function calls more readable and flexible. This technique uses object destructuring to extract named properties from the passed argument. Syntax function functionName({ param1, param2, param3 }) { // Function body } // Call with named arguments functionName({ param1: value1, param2: value2, param3: value3 }); Basic Example Named Arguments Example Named ...
Read MoreNesting template strings in JavaScript
Nesting template strings in JavaScript allows you to embed one template literal inside another. This is useful when building complex strings with dynamic content or when passing template literals as function arguments. What are Nested Template Strings? Nested template strings occur when you place one template literal (using backticks) inside another. The inner template string is evaluated first, then the outer one. Basic Syntax `Outer template ${`Inner template ${variable}`} continues here` Simple Example Nested ...
Read More