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 271 of 534
Searching for minimum and maximum values in an Javascript Binary Search Tree
In this article, we will explain how to find the minimum and maximum values in a binary search tree (BST), with implementation in JavaScript. A binary search tree is a data structure that stores data in a sorted order such that for every node, the left subtree contains values less than the node's value, and the right subtree contains values greater than the node's value. So the leftmost node will have the minimum value, and the rightmost node will have the maximum value. Find Minimum and Maximum in a BST Given root node of a binary search ...
Read MoreHow to create a session only cookies with JavaScript?
Session cookies are temporary cookies that expire when the browser session ends (when the user closes the browser). Unlike persistent cookies, they don't have an expiration date set and are automatically deleted when the browser is closed. Creating Session Cookies To create a session cookie in JavaScript, simply omit the expires or max-age attribute when setting the cookie: // Session cookie - no expiration date document.cookie = "sessionUser=johnDoe; path=/"; // Another session cookie with additional attributes document.cookie = "tempData=someValue; path=/; secure; samesite=strict"; console.log("Session cookies created"); console.log("Current cookies:", document.cookie); Session ...
Read MoreHow to get a string representation of a number in JavaScript?
Use the toString() method to get the string representation of a number. This method converts any number to its string equivalent and supports different number bases for advanced use cases. Syntax number.toString() number.toString(radix) Parameters radix (optional): An integer between 2 and 36 representing the base for numeric representation. Default is 10 (decimal). Basic Example var num1 = 25; ...
Read MoreHow to get a decimal portion of a number with JavaScript?
In JavaScript, you can extract the decimal portion of a number using several methods. The most common approach is using the modulo operator (%) with 1. Using the Modulo Operator (%) The % operator returns the remainder after division. When used with 1, it gives the decimal part: var num1 = 5.3; var num2 = 4.2; var num3 = 8.6; document.write(num1 ...
Read MoreHow to make an anchor tag refer to nothing?
To make an anchor tag refer to nothing, use javascript:void(0). The following link does nothing because the expression "0" has no effect in JavaScript. Here the expression "0" is evaluated, but it is not loaded back into the current document. Using javascript:void(0) The javascript:void(0) approach prevents the default link behavior and returns undefined, making the link inactive. Inactive Anchor Tag Click the following, This won't react at all... Click me! Alternative Methods There are ...
Read MoreHow to display JavaScript variable value in alert box?
To display JavaScript variable values in an alert box, you can pass variables directly to the alert() function. This is useful for debugging or showing information to users. Basic Syntax alert(variableName); alert("Text: " + variableName); alert(`Template: ${variableName}`); Example: Displaying Single Variable function showSingle() { var message = "Hello World!"; alert(message); } ...
Read MoreWhat is the difference between parseInt(string) and Number(string) in JavaScript?
In JavaScript, parseInt() and Number() both convert strings to numbers, but they handle invalid characters differently. Understanding their behavior is crucial for proper string-to-number conversion. parseInt() Method The parseInt() method parses a string character by character and stops at the first non-digit character, returning the parsed integer portion. console.log(parseInt("765world")); console.log(parseInt("50px")); console.log(parseInt("123.45")); console.log(parseInt("abc123")); 765 50 123 NaN Number() Method Number() attempts to convert the entire string to a number. If any part of the string is invalid, it returns NaN. console.log(Number("765world")); console.log(Number("50px")); console.log(Number("123.45")); console.log(Number("123")); NaN NaN ...
Read MoreHow to get the first index of an occurrence of the specified value in a string in JavaScript?
To get the first index of an occurrence of the specified value in a string, use the JavaScript indexOf() method. This method returns the position of the first occurrence of the specified substring, or -1 if not found. Syntax string.indexOf(searchValue, startPosition) Parameters searchValue: The substring to search for (required) startPosition: The index to start searching from (optional, default is 0) Return Value Returns the index of the first occurrence of the specified value, or -1 if the value is not found. Example You can try to run the following code ...
Read MoreHow to define integer constants in JavaScript?
ECMAScript allows usage of const to define constants in JavaScript. To define integer constants in JavaScript, use the const keyword. Syntax const CONSTANT_NAME = value; Example const MY_VAL = 5; console.log("MY_VAL:", MY_VAL); // This will throw an error try { MY_VAL = 10; } catch (error) { console.log("Error:", error.message); } MY_VAL: 5 Error: Assignment to constant variable. Key Points As shown above, MY_VAL is a constant with value 5 assigned. Attempting to reassign another value to a constant ...
Read MoreHow to format JavaScript date into yyyy-mm-dd format?
To format a JavaScript date into "yyyy-mm-dd" format, you can use several methods. The most common approaches are using toISOString() with substring extraction or building the format manually. Using toISOString() Method The toISOString() method returns a date in ISO 8601 format. To get just the "yyyy-mm-dd" part, extract the first 10 characters: JavaScript Date Formatting var date = new Date(); var formattedDate = date.toISOString().substring(0, 10); ...
Read More