args js code example
Example 1: arrondi js
let x = 2.960024578;
let res = x.toFixed(2);
console.log(res);
Example 2: js unspecified parameters
function my_log(...args) {
console.log(args);
console.log(...args);
}
Example 3: js array
var colors = [ "red", "orange", "yellow", "green", "blue" ];
console.log(colors);
console.log(colors[0]);
console.log(colors[1]);
console.log(colors[4]);
colors[4] = "dark blue"
console.log(colors[4]);
Example 4: arguments object in javascript
var sum = 0;
function addAll(){
for (var i = 0; i<arguments.length; i++){
sum+=arguments[i];
}
console.log(sum);
}
addAll(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
Example 5: *args python javascript equivalent
a = [1, 2, 3]
function printArgs(args) {
console.log(...args)
}
printArgs(a)
Example 6: how to access any argument in javascript
function example() {
console.log(arguments);
console.log(arguments[0]);
}
example('hi', 'hello');