I'm learning Javascript in my spare time. So when I learnt the possibility of using varargs in Javascript, I wanted to share it. Here we go;
You probably know how functions are defined in Javascript:
3 | document.write( "transform function initiated" ); |
It is, of course, possible to define functions with arguments such as;
1 | function transformWithMessage(message){ |
2 | document.write( "transform function initiated: " +message); |
Now, I'll show functions with indefinite number of arguments;
3 | document.write( "transform function initiated: " ); |
5 | for ( var i=0; i< arguments.length; i++) |
6 | document.write(arguments[i]+ " " ); |
A call such as
transform("first lock engaged", "second lock engaged") will result in an output such as
transform function initiated: first lock engaged second lock engaged . Similarly, a call such as
transform("first", "second", "third") will result in an output such as
transform function initiated: first second third .