Deconstructing arrays for the function arguments [duplicate] - javascript

This question already has answers here:
Passing an array as a function parameter in JavaScript
(12 answers)
Closed 6 years ago.
I have
function f(a,b,c) {}
and
const arr = [1,2,3]
What do I need to call
f(arr)
__________________________________________?

One this you can do is:
f.apply({}, arr);

Related

How is this string as a function argument working (no parentheses?!)? [duplicate]

This question already has answers here:
Backticks (`…`) calling a function in JavaScript
(3 answers)
Closed 1 year ago.
Is this a new way to call a function? Whats this even called? Why do this?
const foo = a => console.log(a)
const k = foo`stuff here?` //whaaaaaaa
//output is ["stuff here?"]
they are called tagged template functions. You can read more on how they work and what they do here: MDN

shift() method is modifying the another array that I don't want to modify [duplicate]

This question already has answers here:
Copy array by value
(39 answers)
Closed 3 years ago.
I only used shift() method for 'arr1[]', but that method modified 'arr2[]', too.
How can fix it?
<script>
var arr1=['a','b','c','d','e'];
var arr2=arr1;
arr2.shift();
alert(arr1);
alert(arr2);
</script>
to have a real assignation do
var arr1=['a','b','c','d','e'];
var arr2 = Object.assign([], arr1);
arr2.shift();
console.log ('arr1:', JSON.stringify(arr1));
console.log ('arr2:', JSON.stringify(arr2));

Use all function arguments without having to enumerate them [duplicate]

This question already has answers here:
Usage of rest parameter and spread operator in javascript
(6 answers)
Closed 4 years ago.
I have functionWithManyArguments(arg1,...,arg15).
Can I log all arguments without having to enumerate them all?
Yes,
You could do something like:-
function foo(...args) {
console.log(...args);
}
foo(1, 2, "a")
...args are rest parameters.

What is the main difference between array and object in js [duplicate]

This question already has answers here:
What’s the difference between “{}” and “[]” while declaring a JavaScript array?
(8 answers)
Closed 4 years ago.
The point of confusion is - when we check the type of array or object using typeof get the return value as Object. So what's the main difference between them.
You can check using constructor.name
const myArr = []
const myObj = {}
console.log(`myArr is an ${myArr.constructor.name}`)
console.log(`my Obj is an ${myObj.constructor.name}`)

What is the difference between `{ }` and `[ ]`? [duplicate]

This question already has answers here:
What is the difference between these arrays?
(5 answers)
Closed 9 years ago.
In JavaScript, what is the difference between
var stack = {};
and
var stack = [];
The first one is an empty object and the second is an empty array.

Categories

Resources