-2

How is the scope of the a variable in those examples? And how it works in general?

javascript

var a = 0;
if(true){
    var a = 1;
    console.log(a);
}
console.log(a);

C

int a = 0;
if(1){
    int a = 1;
    printf("%i ", a);
}
printf("%i ", a);

Python

a = 0
if True:
    a = 1
    print a
print a
iianfumenchu
  • 898
  • 9
  • 14
  • 1
    C is block scoped (global and block). Python has function scope (and class and module scope). JavaScript is function scoped, and doesn't have block scope for regular variables *(but it does for `let` and `const`)* – adeneo Jul 03 '16 at 19:16
  • 1
    What did you find out yourself? What **specifically** did you not understand? There is enough information for each language which can be found by a straight-forwarnd search. – too honest for this site Jul 03 '16 at 19:49
  • 1
    What's the point of this question.The questions have already been asked about each language, e.g. [What is the scope of variables in JavaScript?](http://stackoverflow.com/q/500431/1529630) – Oriol Jul 03 '16 at 19:50
  • @adeneo: Actually Python is object scoped. Functions, classes, etc. are first class objects – too honest for this site Jul 03 '16 at 19:50
  • @Oriol : I know that I can search the answer in the "internet", i tought this website is also to collect questions and answers, and not only to answer question that doesn't exist on the "internet". – iianfumenchu Jul 15 '16 at 17:05
  • ok, you are right this question is a chaos, i open a new one – iianfumenchu Jul 15 '16 at 17:08
  • @iianfumenchu Not sure why you quote "internet" like if I said something about the "internet". You can ask questions whose answers can be found in the "internet". They are allowed, but don't get surprised if they receive downvotes ("this question does not show any research effort" is a valid reason to downvote). What you should try to avoid is asking questions which have already been asked in StackOverflow. – Oriol Jul 15 '16 at 17:21
  • @Oriol Yes you are right. Here I asked and i answered my own question to keep track what I founded already, [Stack Exchange encourage users to answer their own questions](http://stackoverflow.com/help/self-answer). And maybe my question was not so clear... was hard for me to know where my question should start and where the answer should begin. – iianfumenchu Jul 15 '16 at 17:33
  • @Oriol I quoted the "internet", not because your answer. It's because internet itself, i don't know if internet is a place or a medium. On the internet OR Trough the internet. Thanks for your answers. – iianfumenchu Jul 15 '16 at 17:35

2 Answers2

3

The output is:

javascript

var a = 0;
if(true){
    var a = 1;
    console.log(a);  //1
}
console.log(a);      //1

C

int a = 0;
if(1){
    int a = 1;
    printf("%i ", a);  //1
}
printf("%i ", a);      //0

Python

a = 0
if True:
    a = 1
    print a   #1
print a       #1

That because javascript and Python doesn't have block scope; so anything declared in an if block has the same scope as anything declared exactly outside the block (and Python doesn't use declaration).

C have block scope. If you declare a static variable inside a block {...}, the variable is no more accessible outside the block.

The same is for all the block types like for, else, elseif...

In javascript if you want to declare a variable exclusive inside a block you can use let:

Variables declared by let have as their scope the block in which they are defined, as well as in any contained sub-blocks . In this way, let works very much like var. The main difference is that the scope of a var variable is the entire enclosing function

source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

and here a lot of examples about javascript scope: https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/

iianfumenchu
  • 898
  • 9
  • 14
1

In Javascript the output will be 1 both times as var a = 1 sets a to 1 beyond just the if block. In C declaring int a = 1 within the if declares a variable just for the if block so you will get the output 1 and then 0. In python a = 0 initializes the variable, but an assignment within an if in python goes beyond the if block as well so you will get 1 both times.

tl;dr: C has block scoped, Javascript and Python have function scope

Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61