Extra parameters in a self executing function? [duplicate] - javascript

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 1 year ago.
I saw this piece of code in a webpack compiled file
(1,2, function(x) {
console.log(x);
return 4;
})(5);
This seems to execute correctly. I am aware the 5 is a parameter to function(x)
I don't understand what the 1,2 are? How is this valid?

Well, it's because the brackets only returns the function
(thing1,thing2,thing3,thing4....,thingN) would show the last thing on the list(in this case, thingN)
Example
var bracketTest1=(1,3,5,7,9,null)
console.log(bracketTest1) //null
var bracketTest2=(null,1,2,3,4)
console.log(bracketTest2) //4
var bracketTest3=(null,1,2,4,5,function(x){return x})
console.log(bracketTest3) //function(x)
console.log(bracketTest3(Object)) //function Object()
console.log((1,2,3,4,null,undefined)) //prints undefined

Related

output of the javascript snippet? javascript variable declarations [duplicate]

This question already has answers here:
How JS hoisting works within functions?
(4 answers)
Why a variable defined global is undefined? [duplicate]
(3 answers)
Closed last year.
My first guess is that the following code has the output of 10 and 100. However, after running the code I get undefined and 100 and I don't understand why the function doesn't see the first declaration of x..
var x = 10;
function f() {
console.log(x);
var x = 100;
console.log(x);
}
f();

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

Calling javascript function using dot operator [duplicate]

This question already has answers here:
How to observe value changes in JS variables
(3 answers)
Closed 5 years ago.
function getPercCalculated(x){
return (x*9.3)/100;
}
var x = 10;
var perx = getPercCalculated(x);
Instead of this I would like to call the getPercCalculated using dot operator like
var perx = x.getPercCalculated()
Can someone help me..!!
Number.prototype.getPercCalculated= function(){
return (this*9.3)/100;
};
This will attach the getPercCalculated to every number in your code tough

How functions create new functions in Javascript? [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
In the Eloquent Javascript book I came across this code.
I understood how this works and the passing of arguments but what I am unable to understand is author's statement regarding this code that it's a function which can create another function!
My question is: How is it creating a new function? What is happening which the author is calling creation of a new function? I mean sure we are creating a function called greaterThan and it has another function in it but I can't see how greaterThan is creating another function!
I assure you I have read many similar Qs before asking but couldn't find the answer I am looking for. Thank you for your time & help.
function greaterThan(n) {
return function(m) {
return m > n;
};
}
var greaterThan10 = greaterThan(10);
console.log(greaterThan10(11));
// → true
The function is being created on the sixth line.
var greaterThan10 = greaterThan(10);
This creates a function greaterThan10 which can be used to check if numbers are greater than 10. You can see it used on line 7.
Edit:
When the function greaterThan is called on line 6, it returns the nested function, effectively making
greaterThan10 = function(m){
return m > 10;
};
The author was calling greaterThan10 a 'new function' created by the function greaterThan.

About javascript function [duplicate]

This question already has answers here:
JavaScript plus sign in front of function expression
(4 answers)
Closed 9 years ago.
I found a strange behaviour in Javascript:
function() {
return 10;
}();
This construction doesn't work on all browser because it has a syntax error. But this construction works (returns ten):
+function() {
return 10;
}();
Why?
The + lets the js engine make the difference between this function expression and a function definition.
For more readability, we usually use
(function() {
return 10;
})();
See related article

Categories

Resources