-1

I have two function they are a function declaration

function myFunction(){ 
   console.log("3");
}
myFunction(); // prints "7"

function myFunction(){
 console.log("7");
};
myFunction(); // prints "7"

I find defarent result when i make two function expression with var keyword

var myFunction = function(){ 
  console.log("3");
}  
myFunction(); // prints "3"

var myFunction = function(){
  console.log("7");
};
myFunction(); // prints "7"

Some where i read about hoisting there say's js function declaration are hoisted and they gonna top of code. And js function make a object as the same name that function has.And i see that if there are 2 same name variable make with var keyword they overwrite each other. ok now when i try this

let myFunction = function(){ 
 console.log("Old");
}
myFunction(); //Error:myFunction already declared

let myFunction = function(){
 console.log("New");
};
myFunction();

so my question is there is the function declaration when run in the interputer and interputer make object as the function name who it create it with var or let

Abdur Rahaman
  • 197
  • 2
  • 12
  • "defarent", "interputer"? – Bergi Apr 15 '18 at 12:09
  • I aks how interputer call function declaration with var or let keyword.Is it called with let why in can't throw an error – Abdur Rahaman Apr 15 '18 at 12:11
  • possible duplicate of [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/q/336859/1048572) – Bergi Apr 15 '18 at 12:11
  • Neither. A function is declared with `function` keyword. – Bergi Apr 15 '18 at 12:12
  • no, when our function code run on interputer. interputer first make a variable as it name.Like (var/let = myFunction); Than make a object with typeof function.I read in a blog – Abdur Rahaman Apr 15 '18 at 12:14
  • Yes, the *interpreter* creates and initialises a variable according to the rules of the `function` keyword. There are neither `var` nor `let` involved. – Bergi Apr 15 '18 at 12:16
  • You might want to link the blog where you read something that you want to ask about. – Bergi Apr 15 '18 at 12:17
  • I am not clear.There are three way make a variable "let/var/const".How you can make a variable with function keyword. – Abdur Rahaman Apr 15 '18 at 12:19
  • Yes, your writing is unclear. No, there are not only three ways to make variables. – Bergi Apr 15 '18 at 12:20
  • you can give me any article about some other way declare a variable.I am not clear – Abdur Rahaman Apr 15 '18 at 12:26
  • If your wondering why you get a result `7 7`, and then `7 3`. Actually has nothing to do with hoisting, but the fact in the second example your doing an assignment for the function just before execution. On your second example too, you would also drop the `var` is makes no sense to re-declare. – Keith Apr 15 '18 at 12:28
  • @rahamanabdurar I dunno, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function? – Bergi Apr 15 '18 at 12:28

2 Answers2

1

I will try to explain the different between the three approaches you mentioned

function keyword

declaring functions by function keyword is hoisted so when you declare tow functions with the same name it return the result of the second one only because the function declaration move to the top of the code (before you call it)

so the code you wrote is

function myFunction(){ 
  console.log("3");
}
myFunction(); // prints "7"

function myFunction(){
 console.log("7");
};
myFunction(); // prints "7"

become like this in run-time:

function myFunction(){ 
  console.log("3");
}
function myFunction(){
 console.log("7");
};

//functions declarations become first 


myFunction(); // prints "7"


myFunction(); // prints "7"

var keyword

any variable declared with var keyword is moved to the top of your code in the run-time with value undefined and then assigned to its value when the interpreter reach this line of code so here we have 2 variables at the start with undefined value the we assign the first function and called it (print 3) then we override it with new value and called it again (print 7)

let keyword

let is block scoped variable and can't be declared twice with the same block so it won't work

Peter Wilson
  • 4,150
  • 3
  • 35
  • 62
  • ok i understand your var and let part,But no function keyword .I read function are object in js.so my question is who interputer make this object with var or let keyword; – Abdur Rahaman Apr 15 '18 at 12:30
  • I added an example to my answer check it – Peter Wilson Apr 15 '18 at 12:36
  • `interputer` I'm assuming you mean `interpreter` , ironically most Javascript engine are not really `interpreter's` nowadays, there more JIT based. Anyway, I can understand that English is not your native tongue, but this question. `who interputer make this object with var or let keyword` is very confusing, and I don't think I'm alone in struggling to understand what your asking, could you try re-wording and we go from there.? ps, I gave a reason why your results were different in the comments at top, are you struggling with that part? – Keith Apr 15 '18 at 12:45
  • @Keith kindly add your comment into the original post not in the answer – Peter Wilson Apr 15 '18 at 12:49
  • thanks, But then how MDN says function is a object.Your example says it still as function. – Abdur Rahaman Apr 15 '18 at 12:50
  • It would make no sense doing that, as I'm replying to @rahamanabdurar comment on your post. – Keith Apr 15 '18 at 12:51
  • @rahamanabdurar the data types in js is tow divisions (primitive data types like string number and boolean ) and object so any type expect primitives is an object (array is an object and function is an object) – Peter Wilson Apr 15 '18 at 12:52
  • @Keith so please tag him in your comment because we all get notified with your comments – Peter Wilson Apr 15 '18 at 12:53
  • Yes, I did mean to do that, but now it's too late. :) But to be honest it surely was obvious I was commenting on @rahamanabdurar . Your certainly not struggling with English :) – Keith Apr 15 '18 at 12:53
0

The problem is actually not with hoisting but with redeclaring let values. What you can't do is:

let myFunction  = function () {};
let myFunction  = function () {}; // Uncaught SyntaxError: Identifier 'myFunction' has already been declared

So you don't have error in line four where you execute function but in line six where you are redeclaring function.

adelura
  • 556
  • 1
  • 5
  • 12