arguments
object behave different in strict and non strict mode. In non-strict mode, the argument
object will reflect the changes in the value of the parameters which are present, however in strict mode any changes to the value of the parameter will not be reflected in the argument
object.
function add(a, b){
console.log(arguments[0], arguments[1]); // Prints : 1,2
a = 5, b = 10;
console.log(arguments[0], arguments[1]); // Prints : 5,10
}
add(1, 2);
For the above code, the arguments
object is changed when we change the value of the parameters. However, for strict mode, the same will not be reflected.
function add(a, b) {
'use strict';
console.log(arguments[0], arguments[1]); // Prints : 1,2
a = 5, b = 10;
console.log(arguments[0], arguments[1]); // Prints : 1,2
}
It's worth noting that, if any one of the parameters is undefined
, and we try to change the value of the parameter in both strict-mode or non-strict mode the arguments
object remains unchanged.
Strict mode
function add(a, b) {
'use strict';
console.log(arguments[0], arguments[1]); // undefined,undefined
// 1,undefined
a = 5, b = 10;
console.log(arguments[0], arguments[1]); // undefined,undefined
// 1, undefined
}
add();
// undefined,undefined
// undefined,undefined
add(1)
// 1, undefined
// 1, undefined
Non-Strict Mode
function add(a,b) {
console.log(arguments[0],arguments[1]);
a = 5, b = 10;
console.log(arguments[0],arguments[1]);
}
add();
// undefined,undefined
// undefined,undefined
add(1);
// 1, undefined
// 5, undefined