0

How does the function run even though i have not called it

function sayHello(name) { var text = 'Hello ' + name; var say = function() { console.log(text); } say(); } var x = sayHello('Joe');

19th May 2018, 6:35 AM
IZzZI
IZzZI - avatar
11 ответов
+ 1
3) when a function is called before declaration of the function // it uses it as the main function and vice versa if uses as the secondary /sub function //if multiple declaration of sayNumber() then if there is only one function with saynumber(); then depends on only one function declared
19th May 2018, 9:04 AM
Rajeeb
0
when the program gets the first name in anywhere inside after declaration of a function, the compiler knows program is calling the function example:- declared anywhere
19th May 2018, 7:12 AM
Rajeeb
0
Then how do I assign sayHello('Joe') to X without calling the function
19th May 2018, 7:47 AM
IZzZI
IZzZI - avatar
0
use different name , do not use the name of the function
19th May 2018, 7:49 AM
Rajeeb
0
Can you give me an example
19th May 2018, 7:50 AM
IZzZI
IZzZI - avatar
0
var y= // anything else
19th May 2018, 7:52 AM
Rajeeb
0
@Rajeeb function say667() { var num = 42; var say = function() { console.log(num); } num++; return say; } var sayNumber = say667(); sayNumber(); //Output: 43 function say667() { var num = 42; var say = function() { console.log(num); } num++; return say; } var sayNumber = say667(); //Output: Blank 1. Why does num = 43(and not 42) although ++ operator is after the function say()? 2. Why is there no output when I removed sayNumber(); 3. What does sayNumber(); exactly do 4. Why is there a necessity to have return say; I am a newbie and i have a lot of questions, please bear with me! It will really make my day if you answer these questions for me
19th May 2018, 8:29 AM
IZzZI
IZzZI - avatar
0
1) multiple declaration of functions sayNumber() , where one function will affect the other the variable num gets affected twice in the code // num= 43 not 42
19th May 2018, 8:44 AM
Rajeeb
0
2) // no output no call for the function second time at the end of second function saynumber
19th May 2018, 8:48 AM
Rajeeb
0
4) return necessity to return a value to the function no necessary unless you provide arguments to the functions
19th May 2018, 8:50 AM
Rajeeb
0
Can you elaborate Number 1. Thank You
19th May 2018, 3:11 PM
IZzZI
IZzZI - avatar