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
Web Development Articles
Page 420 of 801
JavaScript program to take in a binary number as a string and returns its numerical equivalent in base 10
We are required to write a JavaScript function that takes in a binary number as a string and returns its numerical equivalent in base 10. Therefore, let's write the code for the function. This one is quite simple, we iterate over the string using a for loop and for each passing bit, we double the number with adding the current bit value to it like this − Example const binaryToDecimal = binaryStr => { let num = 0; for(let i = 0; i < binaryStr.length; i++){ ...
Read MoreJavaScript Auto-filling one field same as other
Auto-filling form fields is a common requirement in web forms, especially when users need to copy address information between billing and shipping sections. This can be achieved using JavaScript event listeners and form field manipulation. Example body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; padding: 20px; } ...
Read MoreJavaScript Const
The JavaScript const declaration creates variables that cannot be reassigned to another value or redeclared later. It was introduced in ES2015 (ES6) and provides block scope like let but with immutable binding. Syntax const variableName = value; Basic const Example body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } Const Example Try ...
Read MoreJavaScript Compare two sentences word by word and return if they are substring of each other
The idea here is to take two strings as input and return true if one string is a substring of the other, otherwise return false. For example: isSubstr('hello', 'hello world') // true isSubstr('can I use', 'I us') // true isSubstr('can', 'no we are') // false In the function, we check which string is longer and then verify if the shorter string exists as a substring within the longer one. Syntax const isSubstr = (first, second) => { if (first.length > second.length) { ...
Read MoreJavaScript Convert an array to JSON
An array is a special object in JavaScript that stores multiple types of values, including integers, strings, and floats simultaneously. JSON (JavaScript Object Notation) is a lightweight data format used to represent structured data, commonly used for transmitting data between servers and web applications. In this article, we'll explore how to convert a JavaScript array into JSON format using the JSON.stringify() method. Understanding JSON.stringify() The JSON.stringify() method converts a JavaScript value or object into a JSON string. Since arrays are objects in JavaScript, we can pass an array as an argument to this method. Syntax ...
Read MoreJavaScript – Getting Coordinates of mouse
In JavaScript, you can get the coordinates of the mouse cursor using mouse event properties. The most common approach is to use the mousemove event listener along with event properties like clientX, clientY, pageX, and pageY. Mouse Coordinate Properties Different properties provide coordinates relative to different reference points: clientX, clientY - Coordinates relative to the viewport (visible browser window) pageX, pageY - Coordinates relative to the entire document (includes scrolled areas) screenX, screenY - Coordinates relative to the user's screen offsetX, offsetY - Coordinates ...
Read MoreJavaScript encodeURI(), decodeURI() and its components functions
JavaScript provides four essential functions for handling URI encoding and decoding. The encodeURI() function encodes complete URIs while preserving structural characters, whereas encodeURIComponent() encodes URI components including all special characters. Understanding the Functions The encodeURI() function encodes special characters in a URI except for reserved characters like , / ? : @ & = + $ # which are essential for URI structure. The encodeURIComponent() function encodes URI components by encoding all special characters including the reserved ones. The corresponding decode functions decodeURI() and decodeURIComponent() reverse the encoding process. Syntax encodeURI(uri) decodeURI(encodedURI) encodeURIComponent(uriComponent) decodeURIComponent(encodedURIComponent) ...
Read MoreJavaScript Error message Property
The message property of JavaScript Error objects contains a human-readable description of the error. It's automatically set when an error occurs and can also be customized when creating custom errors. Syntax error.message Example: Accessing Error Message JavaScript Error message Property body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; ...
Read MoreHow to selectively retrieve value from json output JavaScript
We have the following data inside a JSON file data.json: data.json { "names": [{ "name": "Ramesh", "readable": true }, { "name": "Suresh", "readable": false }, { "name": "Mahesh", "readable": true }, { "name": "Gourav", "readable": true }, { "name": "Mike", "readable": false }] } ...
Read MoreJavaScript Error name Property
The name property of JavaScript Error objects identifies the type of error that occurred. It returns a string representing the error's name, which helps in debugging and error handling. Syntax error.name Common Error Names Different error types have specific names: ReferenceError - Variable or function not defined TypeError - Wrong data type used SyntaxError - Invalid syntax RangeError - Number out of range Example: Displaying Error Names Error Name Property ...
Read More