params javascript code example
Example 1: get params js
let params = (new URL(document.location)).searchParams;
let token = params.get("token");
Example 2: javascript function variable arguments
function foo() {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
foo(1,2,3);
Example 3: javascript function multiple parameters
function sum(...values) {
console.log(values);
}
sum(1);
sum(1, 2);
sum(1, 2, 3);
sum(1, 2, 3, 4);
function sum(...values) {
let sum = 0;
for (let i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
}
console.log(sum(1));
console.log(sum(1, 2));
console.log(sum(1, 2, 3));
console.log(sum(1, 2, 3, 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: rest parameters
function f(a, b) {
let normalArray = Array.prototype.slice.call(arguments)
let normalArray = [].slice.call(arguments)
let normalArray = Array.from(arguments)
let first = normalArray.shift()
let first = arguments.shift()
}
function f(...args) {
let normalArray = args
let first = normalArray.shift()
}
Example 6: parameters in javascript
function myFunction(x, y) {
if (y === undefined) {
y = 2;
}
}