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
Programming Articles
Page 889 of 2547
How to use JavaScript to check if a number has a decimal place or it's a whole number?
In JavaScript, determining whether a number is a whole number or has decimal places is a common requirement. This tutorial explores three effective methods to accomplish this task using both built-in JavaScript methods and custom functions. The following methods are available to check if a number is decimal or whole: Using the Math.floor() Method Using the Number.isInteger() Method Using a Custom isDecimal() Function Using Math.floor() Method The Math.floor() method returns the largest integer less than or equal to a given number. By comparing the original number with its ...
Read MoreHow to access a function property as a method in JavaScript?
In JavaScript, object properties can hold functions, which become methods when accessed through the object. A method is simply a function that belongs to an object and can access the object's other properties using the this keyword. Syntax const object = { property1: value1, property2: value2, methodName: function() { return this.property1 + this.property2; } }; // Call the method object.methodName(); Example 1: Employee Object with fullName Method Here's an ...
Read MoreHow to hide e-mail address from an unauthorized user in JavaScript?
Hiding email addresses from unauthorized users helps protect privacy and reduce spam. JavaScript provides several methods to obfuscate email addresses while keeping them readable for legitimate users. Method 1: Partial Masking with Asterisks This approach replaces part of the username with asterisks, keeping the domain visible: function hideEmail(email) { var parts = email.split("@"); var username = parts[0]; var domain = parts[1]; if (username.length 2 ? ...
Read MoreWrite a palindrome program in JavaScript so that only alphanumeric values should be allowed?
A palindrome is a string that reads the same forwards and backwards. When checking palindromes with alphanumeric characters only, we need to ignore spaces, punctuation, and special characters, considering only letters (a-z) and digits (0-9). For example, "A man, a plan, a canal: Panama" becomes "amanaplanacanalpanama" after removing non-alphanumeric characters, which is indeed a palindrome. Basic Palindrome Check Here's a simple approach using string manipulation methods to check if a string is a palindrome: var str = "racecar"; ...
Read MoreHow to call a JavaScript function from C++?
Calling a JavaScript function directly from C++ depends on the environment and the system; for this, make sure you have embedded a JavaScript engine or integrated C++ with JavaScript. In this article, we will be using Emscripten (C++ to JavaScript in WebAssembly) to call a JavaScript function from C++. For this, you have to compile the C++ program to WebAssembly using Emscripten and then call JavaScript functions from C++. So, first, create the C++ file with the header . How Emscripten Works Emscripten compiles C++ code to WebAssembly, which runs in browsers. The compiled WebAssembly module can ...
Read MoreHow to compare Python DateTime with Javascript DateTime?
Both Python and JavaScript have unique ways of representing date and time data. To compare Python datetime objects with JavaScript Date objects, we must ensure that both are converted to a common format, such as ISO 8601 strings or Unix timestamps (milliseconds since epoch). The following are two major differences between Python datetime and JavaScript Date objects: Month Representation: JavaScript uses a 0-indexed month (0 for January, 11 for December), while Python uses a 1-indexed month (1 for January, 12 for December). Default Time Zone: Python defaults to UTC, ...
Read MoreJavaScript variables declare outside or inside loop?
In JavaScript, variables can be declared either inside or outside loops without performance differences. However, the choice affects variable scope and code readability. Variable Scope: var vs let With var, variables are function-scoped regardless of where declared. With let, variables are block-scoped, meaning they're only accessible within their block. Example: Variable Declared Outside Loop When declared outside, variables have broader scope and can be accessed throughout the function: function myFunction1() { var i; // Function scope - can be used throughout the function ...
Read MoreWhat's the difference between window.location and document.location?
In JavaScript, both window.location and document.location provide access to the current page's URL information, but they have subtle differences in browser compatibility and usage patterns. The window.location Property The window.location property is a reference to a Location object that represents the current URL of the document being displayed in that window. Since the window object is at the top of the scope chain, you can access location properties without the window prefix (e.g., location.href instead of window.location.href). Example: Getting Current URL JavaScript Get Current URL ...
Read MoreHow to remove the hash from window.location (URL) with JavaScript without page refresh?
The replaceState() method replaces the current history entry with the state objects, title, and URL passed as method parameters. This method is useful when you want to update the current history entry's state object or URL in response to a user action. To remove the hash from a URL without triggering a page refresh, we can use the HTML5 History API's replaceState() method. This approach modifies the browser's address bar while keeping the user on the same page. Syntax The syntax for the replaceState() method is as follows: window.history.replaceState(stateObj, title, url) Parameters ...
Read MoreConvert Image to Data URI with JavaScript
JavaScript provides several methods to convert images to Data URI (Base64) format. A Data URI is a string representation of an image that can be embedded directly in HTML or CSS. This tutorial covers two main approaches: using Canvas API for image URLs and FileReader API for local files. Method 1: Converting Image URL to Data URI Using Canvas This method uses the HTML5 Canvas API to convert an image from a URL to a Base64 Data URI string. Convert Image URL to Data URI ...
Read More