🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this chapter, we will explore the Math.min() method in TypeScript. This method returns the smallest of zero or more numbers. Understanding how to use Math.min() is useful for finding the minimum value in a set of numbers.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The Math.min() method returns the smallest of zero or more numbers. If no arguments are given, the result is Infinity. If any argument is not a number, the result is NaN.
2. Syntax
Math.min(value1, value2, ..., valueN);
Parameters
value1, value2, ..., valueN: Zero or more numbers among which the smallest value is to be found.
Return Value
The method returns the smallest of the given numbers. If no arguments are provided, it returns Infinity. If any argument is NaN, it returns NaN.
3. Examples
Let's look at some examples to understand how Math.min() works in TypeScript.
Example 1: Basic Usage
In this example, we use Math.min() to find the smallest number among a set of numbers.
let result = Math.min(10, 20, 30, 40, 50);
console.log(result); // Output: 10
Example 2: Using Negative Numbers
In this example, we use Math.min() to find the smallest number among a set of positive and negative numbers.
let result = Math.min(-10, -20, -30, 5, 0);
console.log(result); // Output: -30
Example 3: Using Math.min() with Variables
In this example, we use Math.min() with variables to find the smallest value.
let num1: number = 15;
let num2: number = 25;
let num3: number = 5;
let result = Math.min(num1, num2, num3);
console.log(result); // Output: 5
Example 4: Using Math.min() with Arrays (Using Spread Syntax)
In this example, we use Math.min() to find the smallest value in an array using the spread syntax.
let numbers: number[] = [10, 20, 30, 40, 50];
let result = Math.min(...numbers);
console.log(result); // Output: 10
Example 5: Using Math.min() with No Arguments
In this example, we use Math.min() with no arguments to see the behavior.
let result = Math.min();
console.log(result); // Output: Infinity
Example 6: Using Math.min() with Non-Numeric Arguments
In this example, we use Math.min() with non-numeric arguments to see the behavior.
let result = Math.min(10, 20, "30", "forty");
console.log(result); // Output: NaN
4. Conclusion
In this chapter, we explored the Math.min() method in TypeScript, which is used to return the smallest of zero or more numbers. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Math.min() effectively can help in various mathematical calculations and scenarios where finding the minimum value is required.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment