Found 128 Articles for TypeScript

How to get substring in TypeScript?

Shubham Vora
Updated on 03-Jan-2023 11:51:17

407 Views

The string contains various characters, and the substring is the part of the string. We can get the substring from the string two ways. The first is using the starting and ending position of the substring, and the second is using the starting position and length of the substring. We will learn both ways to get the substring from the string in TypeScript in this tutorial. Get The Substring Using The Start Position and End Position In TypeScript, indexing of string starts from zero. So, if we have a zero-based starting and ending position of the substring, we can get ... Read More

How to find the natural logarithm of a number in TypeScript?

Shubham Vora
Updated on 03-Jan-2023 11:48:34

270 Views

The natural logarithm is the logarithm of any numeric value to the base e. Here e is Euler's constant, and the value of Euler’s constant is approximately 2.718. In TypeScript, we can use the built-in library methods to find the natural logarithm of any numeric value greater than or equal to zero. Using the Math.log() Method Math is a library of TypeScript which contains all the methods to perform mathematical operations. Inside the Math object, all methods are static. So, we can directly access all methods by taking Math (object name) as a reference. The math method also contains the ... Read More

How to create an array of objects in TypeScript?

Shubham Vora
Updated on 03-Jan-2023 11:46:04

13K+ Views

In TypeScript, the array contains the data or different values and can also contain an object. The object contains the properties and methods in TypeScript. We can access the object properties and invoke the object method by taking the object as a reference. In this tutorial, we will learn to create an array of multiple objects in TypeScript. We will also learn to perform some operations, such as sorting on the array of objects. Syntax Here, we have given the syntax to follow to create the array of objects. let obj_array: Array = [{object properties}] In the above syntax, ... Read More

Explain Enum in TypeScript

Shubham Vora
Updated on 03-Jan-2023 11:39:00

442 Views

Before getting into “enum, ” it is essential to know about the popular keyword “const” in typescript. When we declare a variable "const", we cannot change the value assigned to it. So, enums are nothing but a collection of these const data types. We can create enums with the help of the "enum" keyword. Enum is an abbreviation for Enumerations, and each constant variable declared under the enum is called a member of that enum. In this article, we will learn about enums in typescript, their features, and the major types of enums. Features of enum in TypeScript Enums ... Read More

How to find last occurence of element in the array in TypeScript?

Shubham Vora
Updated on 03-Jan-2023 11:33:46

2K+ Views

We will learn to find the last index of the element in the array in TypeScript. In development, an array of the data can contain duplicate data, and we may need to keep the last occurrence of the element. For example, we have fetched all users’ login history from the database. Now, we want to find when particular users were logged in for the last time. In such scenarios, we can use the below methods to find the last occurrence of the element in the array. Search from the last in the array To find the last occurrence of the ... Read More

What are type guards in typescript?

Shubham Vora
Updated on 19-Dec-2022 16:20:34

3K+ Views

In TypeScript, the type guards are used to determine a variable's type, often inside a conditional or functional block. The type guards usually take the variable and return a Boolean value or the variable type. Type guards allow you to tell the TypeScript compiler to infer a given type for a variable in a specific context, guaranteeing that an argument's type is what you say it is. Like feature detection, type guards are frequently used to limit a type and let you identify the proper prototypes, methods, and attributes of a value. As a result, handling that value became simple ... Read More

Method Overriding in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 16:17:06

9K+ Views

The method overriding means writing the new definition of the method in the inherited class by keeping the method name, parameters, and return type the same. In this tutorial, we will learn about the method overriding in TypeScript. Inheritance is one of the four pillars of object-oriented programming. The method overriding feature of the OOPs is helpful with the inheritance. For example, we can define the method in the parent class and redefine the method with the same name, parameter, and return type into the base class with a different code to execute, and that’s how method overriding comes into ... Read More

How to use getters and setters in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 16:06:17

7K+ Views

In TypeScript, the getters and setters are two terms that can be used to get and set the value of the class members, respectively. However, users can access the public members of the class directly via the dot operator by taking the object of the particular class as a reference. To access the class's private members, we just need to use the getter method. In this tutorial, we will learn to use the getters and setters in TypeScript. Using the getters to access the private members of the class As we create a method to access the class's private ... Read More

How to sort a 2D array in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 16:03:46

4K+ Views

In TypeScript, a 2D array is an array of arrays, where each inner array represents a row of the 2D array. You can access individual elements of the array using bracket notation, with the row and column indices separated by a comma. A single-dimensional array can store values of any data type, such as string, number, objects, etc. To sort a 2D array in TypeScript, you can use the sort() method along with a custom compare function. The sort() method takes the compare function as an optional argument, and the compare function should take two arguments, a and b, ... Read More

How to push an element to the last of an array in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 16:02:09

2K+ Views

In TypeScript, an array is a data type that represents a collection of elements. Each element in the array has a specific index, or position, in the array, and you can access or modify the elements in the array using their index. In TypeScript, an array can contain elements of the same or different data types. To push an element to the end of an array in TypeScript, you can use the push() method. This method adds the element to the end of the array and returns the new length of the array. Syntax Users can follow the syntax below ... Read More

Advertisements