For loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

For loop

Hi everyone, What is the difference between these var i ; for ( i = 0; i < 10; i++){ } Vs for(var i = 0; i < 10; i++){ } Vs for(i = 0; i < 10; i ++){ }

20th Jun 2021, 5:58 AM
Mawande Notununu
Mawande Notununu - avatar
8 Answers
+ 4
A.S. if javascript, the third one is valid, even if unadvised ;P Mawande Notununu specify the language if you want some chance to get accurate answer ^^
20th Jun 2021, 6:27 AM
visph
visph - avatar
+ 4
A.S. Third one is working fine https://code.sololearn.com/cE2szvKegQ3z/?ref=app Hi, Mawande Notununu the purpose of all three ways are same you can use any one!
20th Jun 2021, 6:27 AM
Abhiyantā
Abhiyantā - avatar
+ 4
in js all three are same... at least if in global scope: in local scope the third will implicitly declare 'i' variable in global scope ^^ however this one is slightly different: for (let i=0; i<10; i++) { } as let variables are block scoped ;P however, no difference in efficiency ;)
20th Jun 2021, 6:32 AM
visph
visph - avatar
+ 3
Mawande Notununu Node.js is an open source, cross-platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux. By Tutorials point
20th Jun 2021, 6:35 AM
Abhiyantā
Abhiyantā - avatar
+ 1
Sorry its JS
20th Jun 2021, 6:28 AM
Mawande Notununu
Mawande Notununu - avatar
+ 1
Rishav Tiwari visph sorry actully i dont have much more idea of node i thought this will be invalid if we dont declear variable and its not valid in c cpp java as well as in js also. Thanks for telling this i will edit my answer .
20th Jun 2021, 6:30 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Thank very much everyone
20th Jun 2021, 6:35 AM
Mawande Notununu
Mawande Notununu - avatar
0
Third one is invalid becz i variable is no decleared . First and second are valid in c ,cpp and java . please mention language also becz in some language u dont need to declear variable . but all three loops will print values from 0-9 if u will write print statement inside loop body
20th Jun 2021, 6:25 AM
A S Raghuvanshi
A S Raghuvanshi - avatar