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
Front End Technology Articles
Page 387 of 652
How to convert a value to a number in JavaScript?
JavaScript provides several methods to convert values to numbers. Each method handles different types of input and has specific use cases. Let's explore the four main approaches: Number(), parseInt(), parseFloat(), and the unary operator (+). Using Number() Method The Number() method converts any value to a number. It's the most comprehensive conversion method and handles various data types. Convert value to number using Number() Number() Method Examples ...
Read MoreHow to convert a JSON string into a JavaScript object?
JSON (JavaScript Object Notation) is a lightweight data format commonly used for data exchange between servers and web applications. When receiving data from a server, it typically arrives as a JSON string that needs to be converted into a JavaScript object to be usable in your code. Why JSON.parse() Over eval() There are two possible ways to convert a JSON string into a JavaScript object: eval() and JSON.parse(). However, eval() is unsafe and vulnerable to code injection attacks, making it unsuitable for parsing JSON data. JSON.parse() is the recommended and secure method. Syntax JSON.parse(text[, reviver]) ...
Read MoreHow to convert JavaScript objects to primitive data types manually?
In JavaScript, objects can be converted to primitive data types (string, number, boolean) using built-in methods. This conversion is essential for operations that require primitive values rather than object references. JavaScript provides several methods for manual conversion including toString(), toDateString(), and valueOf(). Each method serves different conversion purposes and returns specific primitive types. Using the toString() Method The toString() method converts any JavaScript object to its string representation. This method is available on all objects and returns a string primitive. Syntax object.toString() Example Converting various object types to strings: ...
Read MoreHow to access 'this' keyword inside an arrow function in JavaScript?
The JavaScript 'this' keyword refers to the object it belongs to. Arrow functions handle 'this' differently than regular functions - they inherit 'this' from their surrounding scope rather than creating their own. How Arrow Functions Handle 'this' Arrow functions don't have their own 'this' binding. Instead, they capture the 'this' value from the enclosing lexical scope at the time they are defined. function Student(fname, grade) { this.fname = fname; this.grade = grade; this.details = function() { ...
Read MoreGenerating n random numbers between a range - JavaScript
We are required to write a JavaScript function that takes in a number, say n, and an array of two numbers that represents a range. The function should return an array of n random elements all lying between the range provided by the second argument. Understanding the Problem The challenge is to generate unique random numbers within a specified range. We need two helper functions: one to generate a single random number between two values, and another to collect n unique random numbers. Example Implementation Following is the code − const num = 10; ...
Read MoreAnother method other than a constructor in a JavaScript class?
JavaScript classes support multiple types of methods beyond constructors. While constructors initialize objects, other methods provide functionality and behavior to class instances. The constructor is used to create and initialize an instance of a class. However, classes can contain additional methods that perform specific operations, calculations, or return formatted data. These methods are called on class instances and have access to the object's properties through the this keyword. Types of Methods in JavaScript Classes JavaScript classes support several method types: Instance methods: Called on class instances Static methods: Called on the class itself Getter/Setter methods: ...
Read More"extends" keyword in JavaScript?
In JavaScript, the extends keyword enables class inheritance, allowing you to create child classes that inherit properties and methods from parent classes. This is a fundamental feature of ES6 classes that promotes code reusability and establishes clear hierarchical relationships between classes. Class inheritance allows you to build upon existing functionality without rewriting code. The child class automatically gains access to all public methods and properties of its parent class, while still being able to add its own unique features or override inherited behavior. Syntax class ChildClass extends ParentClass { // Child class ...
Read MoreGetters and setters in JavaScript classes?
In this article we are going to discuss about the getters and setters in JavaScript classes with suitable examples in JavaScript. In JavaScript, getters and setters are special methods that provide controlled access to object properties. They allow you to define custom behavior when getting or setting property values, ensuring data validation and encapsulation. Getters and setters use the get and set keywords respectively. Let's explore how to implement getters and setters in JavaScript classes and objects with practical examples. Syntax The basic syntax for getters and setters: class MyClass { ...
Read MoreWeekday as a number in JavaScript?
In JavaScript, the getDay() method from the Date object returns the weekday as a number from 0 to 6, where Sunday is 0, Monday is 1, and so on. Syntax dateObject.getDay() The method returns an integer representing the day of the week: 0 = Sunday 1 = Monday 2 = Tuesday 3 = Wednesday 4 = Thursday 5 = Friday 6 = Saturday Example 1: Current Date Weekday Get the ...
Read MoreWriting a For Loop to Evaluate a Factorial - JavaScript
We are required to write a simple JavaScript function that takes in a Number, say n and computes its factorial using a for loop and returns the factorial. For example: factorial(5) = 120, factorial(6) = 720 The approach is to maintain a count and a result variable, keep multiplying the count into result, simultaneously decreasing the count by 1, until it reaches 1. Then finally we return the result. Syntax function factorial(n) { let result = 1; for (let i = n; i > ...
Read More