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