Javascript Articles

Page 234 of 534

Conditional Properties Using React with TypeScript

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 6K+ Views

In React with TypeScript, conditional properties allow you to pass props to components only when certain conditions are met. This technique is essential for creating dynamic, interactive user interfaces that respond to state changes and user interactions. TypeScript enhances this pattern by providing type safety, ensuring that your conditional props are correctly typed and helping catch potential runtime errors during development. Understanding Conditional Properties Conditional properties are props that are only set on a component under specific conditions. In React with TypeScript, you can implement this using: Ternary operator - Returns one value if condition ...

Read More

Duck Typing in TypeScript

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 1K+ Views

Duck typing is a programming concept where an object's type is determined by its behavior (methods and properties) rather than its class inheritance. The name comes from the phrase "If it walks like a duck and quacks like a duck, then it must be a duck." What is Duck Typing? Duck typing focuses on what an object can do rather than what it is. If two objects have the same methods and properties, they can be used interchangeably, regardless of their actual class or type. Duck Typing in TypeScript TypeScript implements duck typing through interfaces. An ...

Read More

Find Hypotenuse of a Number In TypeScript

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 419 Views

The longest side of a right-angled triangle and the side that faces away from the right angle is known as the hypotenuse. The Pythagorean theorem states that the hypotenuse's square equals the sum of the squares of the other two sides. The formula is c² = a² + b², where c is the hypotenuse and a and b are the triangle's two sides. In TypeScript, we can create functions to calculate the hypotenuse using the Pythagorean theorem. The function accepts the lengths of the two shorter sides as parameters and returns the hypotenuse length. This only works for right ...

Read More

How to create asynchronous function in TypeScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 7K+ Views

Asynchronous programming allows us to perform multiple tasks without blocking code execution. In TypeScript, we use the async/await keywords to create asynchronous functions that handle promises elegantly. Before diving in, let's understand why asynchronous programming is essential. When fetching data from APIs or performing I/O operations, these tasks take time to complete. In single-threaded languages like TypeScript and JavaScript, synchronous code would block execution until the operation finishes, leading to poor performance. Asynchronous functions solve this by allowing other code to execute while waiting for time-consuming operations to complete. The await keyword pauses execution only within the async ...

Read More

How to solve too many try catch in Typescript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 1K+ Views

We can use the try-catch statements to handle errors in TypeScript. Sometimes, we require adding more than one try-catch block in the code to handle multiple errors. When we add multiple try-catch statements in the code, the code becomes unreadable, and it becomes a headache for developers to refactor. In this tutorial, we will learn to convert too many try-catch blocks into a single try-catch block which can manage multiple errors. Syntax Users can follow the syntax below to use the single try-catch block in TypeScript. try { throw new Error("error_message"); ...

Read More

How to Develop Generic Classes

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 275 Views

Generic classes in TypeScript allow you to create flexible, reusable classes that can work with multiple data types. They use type parameters enclosed in angle brackets () to represent the type of data the class will handle, making your code more versatile and type-safe. A generic class uses type parameters (commonly denoted as "T") as placeholders for actual types. When you instantiate the class, you specify the concrete type, and the class properties and methods adapt accordingly. This approach eliminates code duplication while maintaining type safety. Syntax The basic syntax for creating a generic class in TypeScript ...

Read More

How to Enforce the type of the indexed members of a Typescript object

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 673 Views

TypeScript is a strongly typed, object-oriented programming language built on JavaScript. One of its powerful features is the ability to enforce types of an object's indexed members using index signatures. This allows you to define the shape of objects with dynamic properties while maintaining type safety. An index signature specifies the types for both keys and values in an object. It's particularly useful when you need objects with unknown property names but known value types. Syntax interface ObjectType { [key: string]: ValueType; } // Or inline let objectName: { [key: string]: ...

Read More

How to convert JavaScript datetime to MySQL datetime?

Saurabh Jaiswal
Saurabh Jaiswal
Updated on 15-Mar-2026 2K+ Views

Date time manipulation in JavaScript is important while dealing with databases. JavaScript Date and time are different from the MySQL date and time. JavaScript provides multiple ways to represent Date and time none of these formats are the same as MySQL's date and time format. In this article, we will discuss some approaches to converting JS date and time into MySQL date and time format. First of all, we will understand the difference between Javascript and MySQL date and time formats. Here is an example − Javascript − ISO 8601 Date Format: YYYY-MM-DDTHH:mm:ss.sssZ MySQL ...

Read More

How to convert Object's array to an array using JavaScript?

Saurabh Jaiswal
Saurabh Jaiswal
Updated on 15-Mar-2026 9K+ Views

Converting an array of objects to a flat array is a common task in JavaScript. There are several approaches to extract all values from objects and create a single array containing those values. Let's understand the problem with an example: Given Array of Objects: let carObj = [ { name: "John", car: "Ford" }, { name: "Mark", car: "BMW" }, { name: "Ben", car: "Toyota" } ] Expected Output: ["John", "Ford", "Mark", "BMW", "Ben", "Toyota"] There are multiple ways to achieve this conversion: ...

Read More

How to convert list of elements in an array using jQuery?

Saurabh Jaiswal
Saurabh Jaiswal
Updated on 15-Mar-2026 5K+ Views

We can use jQuery.makeArray() method or jQuery.each() method to convert a list of elements into an array using jQuery. The makeArray() method is the most convenient way to perform this task. This method is used to convert an object into a native array. Using jQuery makeArray() Method The $.makeArray() method in jQuery converts an array-like object into a JavaScript array. It takes a single argument and converts it to an array. Syntax $.makeArray(obj) Here obj is an object that we want to convert to an array. Example In this example we ...

Read More
Showing 2331–2340 of 5,340 articles
« Prev 1 232 233 234 235 236 534 Next »
Advertisements