Found 6685 Articles for Javascript

How to convert a value to a safe integer using JavaScript?

Prince Varshney
Updated on 21-Jul-2022 12:46:49

401 Views

An integer is known to be a safe integer if that can be exactly represented as an IEEE-754 double precision number. A safe integer is an integer in the range -(2^53 - 1) to (2^53 - 1). All these numbers are considered safe integer because it allows the one-to-one mapping of the numbers between the mathematical integers and their representation in JavaScript.SyntaxFollowing the syntax to check for a safe integer −Number.isSafeInteger(testValue)Here testValue is the number to check if it is a safe integer or not.It will return true if testValue is a safe integer else false.Example Check If a Number is a ... Read More

How to check the current runtime environment is a browser in JavaScript?

Prince Varshney
Updated on 26-Jul-2022 08:38:51

3K+ Views

A runtime environment is an environment where your code will be executed. It tells what global objects your code can access and it also impacts its result. A JavaScript code can run in different types of environments some of them are Node.js, Service Workers, or in a web browser. So, to start coding in JavaScript, you don’t have to install any additional software. Each web browser comes with a JavaScript engine. You can simply run scripts you write inside any browser but it ensures that it follows all the rules of the ECMAScript (ES6) functionality.Here we will detect in which ... Read More

How to print colored text in the console using JavaScript?

Prince Varshney
Updated on 21-Jul-2022 13:24:02

2K+ Views

In this article, we will learn how to add colors to the text and print them in the console window of JavaScript. In the original, all the data printed in the console is of black color no other color is reflected in the console but here we are going to add some special characters with text to make our console window look more colorful.There are some special codes that help change in color of the output in the console window and these codes are known as ANSI escape codes. By adding these codes in the console.log() method we can see ... Read More

What is JavaScript Error Constructor?

Prince Varshney
Updated on 21-Jul-2022 10:20:05

993 Views

A JavaScript constructor is a function that creates and initializes an object instance of a class. A constructor is used to create a new object and set values for existing object properties. The Error() constructor in JavaScript is used to create new error objects. Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. See below for standard built-in error types.SyntaxFollowing is the syntax for an Error( ) constructor −new Error() new Error(message) new Error(message, options) new Error(message, fileName) new Error(message, fileName, lineNumber)The Error() constructor can be defined ... Read More

How to replace line breaks with
using JavaScript?

Prince Varshney
Updated on 21-Jul-2022 09:57:07

3K+ Views

In this tutorial, we are going to learn how to replace all the line breaks in JavaScript code using a tag. There are various methods using which we can replace all the line breaks with tag. Some of the methods are given below −Using String replace() Method and RegExIn this method we are using the String.replace() method to replace all the line breaks in the plain text with the tag. Here the RegEx refers to the regular expression that we write inside the string.replace() method.SyntaxFollowing is the syntax to use replace() method −sentence.replace(/(?:\r|\r|)/g, "");Here sentence is the string ... Read More

How to return a passed string with letters in alphabetical order in JavaScript?

Imran Alam
Updated on 01-Jul-2022 13:42:27

2K+ Views

We could apply String split(), Array sort() and Array join() methods to write a function that returns a passed string with letters in alphabetical order in JavaScript. A JavaScript function is a block of code that is executed when it is invoked. A string is a sequence of characters. In JavaScript, strings are represented by the type String.Input string : tutorialspoint Output string: aiilnooprstttuStepsSplit the string into an array of characters using split() method.Apply the sort() method to sort the characters in alphabetical order.Finally, we use the join() method to join the characters back into a string.The Array.sort() method sorts the ... Read More

How to convert array of strings to array of numbers in JavaScript?

Imran Alam
Updated on 15-Sep-2023 02:14:31

27K+ Views

In JavaScript, there are different ways to convert an array of strings to an array of numbers. The first way is to use the built-in parseInt() method, and the second way is to use a type conversion function, and the third one is to use the unary + operator.Using the parseInt() methodThe parseInt() method is a built-in function in JavaScript that takes a string as an argument and returns a number. This method can be used to convert an array of strings to an array of numbers.SyntaxThe syntax of the parseInt() method is as follows −parseInt(string, radix);ParametersString − The string ... Read More

What is difference between unescape() and escape() functions in JavaScript?

Imran Alam
Updated on 01-Jul-2022 13:08:25

2K+ Views

JavaScript provides two functions for dealing with encoded strings: escape() and unescape(). The escape() function is used to encode a string, making it safe for use in a URL. The unescape() function is used to decode an encoded string.The differencesThe main difference between the two functions is that escape() encodes characters that are not ASCII, while unescape() only decodes those characters. This means that if you use escape() on a string that only contains ASCII characters, the result will be the same as the input string. However, if you use unescape() on a string that contains non-ASCII characters, the result ... Read More

What is the difference between undefined and not defined in JavaScript?

Imran Alam
Updated on 01-Jul-2022 13:04:34

6K+ Views

In JavaScript, there are two main ways that a variable can be "undefined". The first is when you declare a variable without giving it a value. The second is when you try to access a variable that does not exist.Undefined in JavaScript When a variable is declared without a value, it is automatically given the value of "undefined". This can happen if you forget to assign a value to a variable, or if you intentionally do not assign a value (For example, if you are waiting for user input).If you try to access a variable that does not exist, you will ... Read More

What is difference between forEach() and map() method in JavaScript?

Imran Alam
Updated on 01-Jul-2022 12:58:19

40K+ Views

JavaScript provides several ways to loop through arrays and objects. The most common way is the for loop, which is used to iterate through the elements of an array or object. However, there are other ways to loop through arrays and objects, such as the forEach() and map() methods.The forEach() MethodThe forEach() method is used to loop through each element of an array or object. The forEach() method takes a callback function as an argument. The callback function is invoked for each element of the array or object.The forEach() method is similar to the for loop, but it does not ... Read More

Advertisements