Understand and use of "param" keyword in TypeScript

Understand and use of "param" keyword in TypeScript

If we are using C# then we can use the "param" keyword and pass the number of arguments to a function, but TypeScript doesn't have the "param" keyword. In TypeScript, we use 3 (three) dots instead of a param keyword with a variable name. The behavior is the same as with-param keywords.

Sometimes we may require a variable number of arguments to be passed to a function; for example, we have a totalsal function which calculates the total salary of all employees, no matter how many employee's salaries are passed.

If we are using C# then we can use the "param" keyword and pass the number of arguments to a function, but TypeScript doesn't have the "param" keyword. In TypeScript, we use 3 (three) dots instead of a param keyword with a variable name. The behavior is the same as with-param keywords.

For example

The "params" keyword is used in C# and in TypeScript 3 (three) . . . (dots) is specified before the variable name and array type.

methodname(... variable_name : type[]){}  
MyMethos(... params : number[]){}  

What's param do

Params enable methods to receive a variable number of parameters. Basically, if you are using params (... variable name) in TypeScript, the argument passed to the method are changed by the compiler into an element in a temporary array and this array is then used in the receiving methods.

The main advantage of param is when designing a library for other programs to widely use. The best example of param is string concatenations.

The following examples show how to use params in TypeScript. Use the following instructions to create a program using params.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window is opened. Provide the name of your application like "params", then click on the Ok button.

Step 2

After Step 1 your project has been created. The Solution Explorer, which is at the right side of Visual Studio, contains the js file, ts file, CSS file, and HTML files.

Step 3

The code of the params program.

params .ts

class Test {  
 Method1(value: string) { // only for single string value  
  alert("" + value);  
 }  
 Method2(...params: number[]) { // Params but number type  
  // var total=0;  
  for (var i = 0; i < params.length; i++) {  
   alert("" + params[i]);  
  }  
 }  
 Method3(...params: any[]) { // params with any type  
  // var total=0;  
  for (var i = 0; i < params.length; i++) {  
   alert("" + params[i]);  
  }  
 }  
}  
window.onload = () => {  
 var data = new Test();  
 data.Method1("Mcn Solution");  
 data.Method2(1, 2, 3, 4, 5, 6);  
 data.Method3(1, "Mcn", 3.3, 'c');  
}  

default.html

<!DOCTYPEhtml>  
<htmllang="en"  
    xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
        <metacharset="utf-8"/>  
        <title>Params in TypeScript</title>  
        <linkrel="stylesheet"href="app.css"type="text/css"/>  
        <scriptsrc="app.js">  
        </script>  
    </head>  
    <body>  
        <h1>Params in TypeScript</h1>  
        <divid="content"/>  
    </body>  
</html>  

app.js

var Test = (function() {  
 function Test() {}  
 Test.prototype.Method1 = function(value) {  
  alert("" + value);  
 };  
 Test.prototype.Method2 = function() {  
  var params = [];  
  for (var _i = 0; _i < (arguments.length - 0); _i++) {  
   params[_i] = arguments[_i + 0];  
  }  
  for (var i = 0; i < params.length; i++) {  
   alert("" + params[i]);  
  }  
 };  
 Test.prototype.Method3 = function() {  
  var params = [];  
  for (var _i = 0; _i < (arguments.length - 0); _i++) {  
   params[_i] = arguments[_i + 0];  
  }  
  for (var i = 0; i < params.length; i++) {  
   alert("" + params[i]);  
  }  
 };  
 return Test;  
})();  
window.onload = function() {  
 var data = new Test();  
 data.Method1("Mcn Solution");  
 data.Method2(1, 2, 3, 4, 5, 6);  
 data.Method3(1, "Mcn", 3.3, 'c');  
};  

Step 4

Output of Method1

It takes only a single string value (simple function):

Output of Method2

It can take n number of arguments but all arguments must be numbers.

Output of Method3

It can take n number of arguments and accepts all types.

Thank you for reading!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

C++ is an incredibly fast and efficient programming language. Its versatility knows no bounds and its maturity ensures support and reliability are second to none. Code developed in C++ is also extremely portable, all major operating systems support it. Many developers begin their coding journey with the language, and this is no coincidence. Being object-oriented means it does a very good job of teaching concepts like classes, inheritance, abstraction, encapsulation and polymorphism. Its concepts and syntax can be found in modern languages like C#, Java and Rust. It provides a great foundation that serves as a high speed on ramp to the more popular, easier to use and modern alternatives.

Now it’s not all rosy. C++ has a very steep learning curve and requires developers to apply best practices to the letter or risk ending up with unsafe and/or poor performing code. The small footprint of the standard library, while most times considered a benefit, also adds to the level of difficulty. This means successfully using C++ to create useful complex libraries and applications can be challenging. There is also very little offered in terms of memory management, developers must do this themselves. Novice programmers could end up with debugging nightmares as their lack of experience leads to memory corruption and other sticky situations. This last point has lead many companies to explore fast performing, safe and equally powerful alternatives to C++. For today’s Microsoft that means Rust.

The majority of vulnerabilities fixed and with a CVE [Common Vulnerabilities and Exposures] assigned are caused by developers inadvertently inserting memory corruption bugs into their C and C++ code - Gavin Thomas, Microsoft Security Response Center
Rust began as a personal project by a Mozilla employee named Graydon Hoare sometime in 2006. This ambitious project was in pre-release development for almost a decade, finally launching version 1.0 in May 2015. In what seems to be the blink of an eye it has stolen the hearts of hordes of developers going as far as being voted the most loved language four years straight since 2016 in the Stack Overflow Developer Survey.

The hard work has definitely paid off. The end result is very efficient language which is characteristically object oriented. The fact that it was designed to be syntactically similar to C++ makes it very easy to approach. But unlike the aforementioned it was also designed to be memory safe while also employing a form of memory management without the explicit use of garbage collection.

The ugly truth is software development is very much a trial and error endeavor. With that said Rust has gone above and beyond to help us debug our code. The compiler produces extremely intuitive and user friendly error messages along with great direct linking to relevant documentation to aid with troubleshooting. This means if the problem is not evident, most times the answer is a click away. I’ve found myself rarely having to fire up my browser to look for solutions outside of what the Rust compiler offers in terms of explanation and documentation.

Rust does not have a garbage collector but most times still allocates and release memory for you. It’s also designed to be memory safe, unlike C++ which very easily lets you get into trouble with dangling pointers and data races. In contrast Rust employs concepts which help you prevent and avoid such issues.

There are many other factors which have steered me away from C++ and onto Rust. But to be honest it has nothing to do with all the great stuff we’ve just explored. I came to Rust on a journey that began with WebAssembly. What started with me looking for a more efficient alternative to JavaScript for the web turned into figuring out just how powerful Rust turns out to be. From its seamless interop…

Automatically generate binding code between Rust, WebAssembly, and JavaScript APIs. Take advantage of libraries like web-sys that provide pre-packaged bindings for the entire web platform. – Rust website
To how fast and predictable its performance is. Everything in our lives evolves. Our smartphones, our cars, our home appliances, our own bodies. C++ while still incredibly powerful, fast and versatile can only take us so far. There is no harm in exploring alternatives, especially one as exceptional and with as much promise as Rust.

What do you guys think? Have you or would you give Rust a try? Let us know your thoughts in the comments section below.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

Why you should move from Node.js to Rust in 2019

Rust Vs. Haskell: Which Language is Best for API Design?

7 reasons why you should learn Rust programming language in 2019

An introduction to Web Development with Rust for Node.js Developers

JavaScript vs C++ vs WebAssembly: Speed, Speed, Speed

JavaScript vs C++ vs WebAssembly: Speed, Speed, Speed

JavaScript vs C++ vs WebAssembly: Speed, Speed, Speed. Learn how JavaScript stacks up against C++ and WebAssembly, what kind of code is actually fast, and the performance considerations one should account for when building apps with web technologies. In this keynote session, Franziska Hinkelmann talks about JavaScript compilers specifically and see how modern JS performance compares to C++ performance. Then, see where WebAssembly fits into this performance story

In Node.js, we can use WebAssembly modules and native C++ addons. If your app has performance critical parts, should you stay in JavaScript? Or write a native C++ addon? Or use WebAssembly? Let’s have a look at how these options compare performance wise and which one is best for different workloads. So the next time you need to optimize for speed, you know your options.

What’s happening under the hood at the compiler level in JavaScript? In this keynote session, Franziska Hinkelmann talks about JavaScript compilers specifically and see how modern JS performance compares to C++ performance. Then, see where WebAssembly fits into this performance story. The concepts that Franziska Hinkelmann will show you are fundamental JS concepts and they apply no matter what framework you are using – so doesn’t matter if you are using Angular, Node.js or anything else.

Moreover, as we go through this journey, the questions like how can dynamically typed JS be so fast, when it became faster than before will be answered as well.

Franziska Hinkelmann spoke at CovalenceConf 2019 about how JavaScript stacks up against C++ and WebAssembly, what kind of code is actually fast, and the performance considerations one should account for when building apps with web technologies.