-2

When I try to run the c++ programme below, it gives me compilation error...While if I try to run the second javascript programme below, it does not give me any error Why?

////c++////

#include<iostream>
using namespace std;

void display(){
    cout << num << endl;  // error: num is not defined in this scope
}

int num = 30;

int main(){
    return 0;
}

/////javaScript////

function display(){
    console.log(num)    // no error
}

let num = 25;
display()

In case of c++ , num variable is not declared at top level that's why it gives me an error....But in case of javaScript 'num' variable is not declared at the top level stil it does not gives any error... Why this happens?

I expected error in js also....

function display(){
    console.log(name)
}

let name = 'hi'
function test(){
    var name = 'Hello'
    display()
}

test()  // output : hi
sparsh goyal
  • 89
  • 1
  • 7
  • 2
    Please read about JavaScript Hoisting. I think then you'll get your answer. https://www.w3schools.com/js/js_hoisting.asp – Mazedul Islam Jul 26 '19 at 21:32
  • 1
    related/dupe: [1](https://stackoverflow.com/questions/28176077/why-does-a-c-need-a-forward-declaration-either-through-a-header-or-a-statement), [2](https://stackoverflow.com/questions/26122024/does-function-order-matter-in-c) – NathanOliver Jul 26 '19 at 21:33
  • 1
    @MohammadMazedulIslam - This isn't really about hoisting in Javascript. It's about the fact that Javascript is interpreted and looks up the value of `num` dynamically when `display()` runs. So, by the time `display()` runs, `num` is in the parent scope and has a value so the interpreter is happy. – jfriend00 Jul 26 '19 at 22:39

1 Answers1

3

Javascript is an "interpreted language" that looks up symbols at function execution time, not at language parse time. That means that num just has to exist and have a value in the appropriate scope when display() is called not when the code is first parsed. The variable num is dynamically looked up at run-time (not resolved at parse time) in the lexical scope. And, since num is defined and initialized in an appropriate scope before display() is called, when the interpreter looks up num in the current scope as it is running display() it finds a variable named num and can use it just fine.

But, C++ is a compiled language that evaluates symbols when the language is parsed/compiled. num must exist at compile time when display() is compiled and it has not yet been declared when the compiler attempts to compile display() so it cannot be found and that creates a compile error. You can, of course, work-around this by changing the declaration of display() to take an int argument and then passing in the value of num as an argument when you call it as in display(num). That will satisfy the compiler.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • ok that's fine but if I called display() function inside another function in which var num has been initialized. Will then js will take that num or global scope num – sparsh goyal Jul 26 '19 at 22:40
  • @sparshgoyal - Javascript has a scope hierarchy, but it is lexical (as the code is declared), not via the call chain. So, `display()` has a fixed lexical scope of the code that it is nested in. Calling `display()` from some other function that has a `num` variable won't affect the scope of `display()`. What affects the scope of `display()` is what it is declared in. If `display()` is declared inside another function that has a `num` variable, then it will find that variable when it does it's variable lookup in the scope hierarchy. – jfriend00 Jul 26 '19 at 22:49
  • Now I edited a little bit, see the below code in the question... you are saying if name variable is declared in the loal scope and also in the global scope then during calling of display() function it will take that local name...so I was expecting output to be hello but the output is Hi – sparsh goyal Jul 26 '19 at 22:51
  • O..Ok Thanks I understand completely what you are saying, thank you so much you solved my problem I was researching it from many hours. Can you suggest me topics to understand all theinterpreter related problems and fixed lexical scoping? – sparsh goyal Jul 26 '19 at 23:00
  • @sparshgoyal - The main thing is to understand what Javascript's "lexical scope" is. There look to be some good articles in this [set of search results](https://www.google.com/search?q=javascript+lexical+scope). In the example you added to your question, `var name` is not in the lexical scope of `display()`. It's in the lexical scope of `test()` and is private to that function. The `let name = 'hi';` is in the lexical scope of `display()` (actually it's in the parent scope, but parent scopes are searched if the symbol isn't found locally). – jfriend00 Jul 27 '19 at 01:15
  • @DaveNewton - I understand what you're saying. It is about how the language works (dynamic symbol lookups at run-time). But, this issue isn't really about hoisting. It would still work even if there wasn't hoisting because the variable declaration and initialization happens before `display()` is called and thus it's fully initialized in the parent scope before `display()` is executed whereas C++ requires that the variable be known at compile time. Javascript only needs it known at execution time. – jfriend00 Jul 27 '19 at 03:51
  • Yep, true. I guess my point was just that compiled/interpreted isn't the hating issue. That was autocorrected, but more entertaining than what I meant. – Dave Newton Jul 27 '19 at 03:53
  • @DaveNewton - I modified the answer based on your feedback. – jfriend00 Jul 27 '19 at 04:07