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 313 of 534
How to convert Boolean to Number in JavaScript?
In JavaScript, Boolean values (true and false) can be converted to numbers (1 and 0 respectively) using several methods. This conversion is useful when performing mathematical operations or comparisons that require numeric values. JavaScript provides multiple approaches to convert Boolean values to numbers. Let's explore three effective methods with examples. Using the Number() Function The Number() function is the most explicit and readable way to convert Boolean values to numbers. It converts true to 1 and false to 0. Syntax let result = Number(booleanValue); Example ...
Read MoreHow to convert a string in to a function in JavaScript?
To convert a string into a function in JavaScript, the eval() method can be used. This method takes a string as a parameter and evaluates it as JavaScript code, which can include function definitions. Syntax eval(string); Example: Converting String to Function In the following example, a string contains a JSON object where the 'age' property holds a function as a string. Using eval(), we convert this string into an actual executable function. var string = '{"name":"Ram", "age":"function() {return 27;}", "city":"New jersey"}'; var fun ...
Read MoreHow to convert Number to Boolean in JavaScript?
This tutorial will teach us to convert a number to a Boolean in JavaScript. The variable of Boolean data type can contain only two values, true and false. When we convert the variables of any other data type to Boolean, it returns true for all non-falsy values and false for all falsy values. Let's understand the falsy values. JavaScript contains 6+ falsy values, and some of them are as below. Null 0 NaN False Undefined ' ' (empty string) ...
Read MoreHow to show the full URL of a document with JavaScript?
We use the URL property of the document object to show the full URL of a document with JavaScript. The document object, part of the DOM, corresponds to the current web page that the browser has loaded. It contains all the information about the condition of the browser as well as the web page. URL stands for Uniform Resource Locator. It contains the address of a resource on the internet. A typical URL looks something like this: http://www.example.com/index.html The Document Object Model of JavaScript acts as a layer of abstraction on the HTML page, creating ...
Read MoreHow to display the title of a document with JavaScript?
In JavaScript, you can display a document's title using two main approaches: the document.title property and the document.getElementsByTagName() method. This tutorial demonstrates both methods with practical examples. Using document.title Property The document.title property is the most straightforward way to access and display an HTML document's title. It directly returns the title as a string. Syntax document.title Example 1 This example shows how to display the document title by clicking a button: This is the title accessed from the document ...
Read MoreCan re-declaring a variable destroy the value of that variable in JavaScript?
Re-declaring a variable will not destroy the value of that variable, until and unless it is assigned with some other new value. If we look at the following example variables "x" and "y" were assigned with values 4 and 8 respectively, later on when those variables were reassigned, the old values were replaced with the new values and displayed as shown in the output. Example: Re-declaring with New Values var x = new Number(4); ...
Read MoreHow to convert Number to String in JavaScript?
This tutorial will teach us to convert the number to string in JavaScript. Changing the type of variable is called the type conversion of a variable. While coding, the programmer needs to deal with the different data types and might need to convert the variable's data type. Every programming language has different methods to convert one variable from one data type to another data type, and JavaScript also has some methods, which are explained below. Using the toString() Method Concatenating with an empty String Using ...
Read MoreHow to force a number to display in exponential notation?
Use the toExponential() method to force a number to display in exponential notation, even if the number is in the range in which JavaScript normally uses standard notation. Syntax number.toExponential(fractionDigits) Parameters fractionDigits - An optional integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number. Return Value Returns a string representing the number in exponential notation. Example: Basic Usage You can force a number to display in exponential notation as follows: ...
Read MoreWhat is the importance of _without() method in JavaScript?
The _.without() method is part of the Underscore.js library that creates a new array by excluding specified values from an original array. It performs case-sensitive matching and removes all occurrences of the specified values. Syntax _.without(array, *values) Parameters array - The source array to filter *values - One or more values to exclude from the array Return Value Returns a new array with the specified values removed, preserving the original array order. Example: Removing Numbers The following example removes multiple numeric values from an array: ...
Read MoreHow to return a string value version of the current number?
The toLocaleString() method returns a string representation of a number formatted according to the user's locale (region and language settings). This method is useful for displaying numbers in a format familiar to users from different countries. Syntax number.toLocaleString() number.toLocaleString(locales) number.toLocaleString(locales, options) Parameters locales (optional): A string or array of strings representing locale identifiers (e.g., 'en-US', 'de-DE') options (optional): An object with formatting options like currency, minimumFractionDigits, etc. Basic Example Here's how to use toLocaleString() with different number formats: JavaScript toLocaleString() Method ...
Read More