Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,143,314 members, 7,780,762 topics. Date: Thursday, 28 March 2024 at 09:27 PM |
Nairaland Forum / Science/Technology / Programming / Javascript— The Dark Parts: The Scope (665 Views)
Are You On The Dark Web? / Were Can I Download Free Video Tutorials On Javascript Or Ruby / Javascript With NodeJS (2) (3) (4)
(1) (Reply)
Javascript— The Dark Parts: The Scope by naijatechworld: 6:17pm On Nov 09, 2014 |
Welcome to this new series of JavaScript tutorials. It is designed for developers who are experienced with other programming languages (C++ or even worst : Java) and who think that JavaScript is a light version of Java: its name starts with Java and it even has a similar syntax which is definitely wrong. I was one day one of these guys. I always had the following issue when writing in JavaScript: I start coding quickly. It’s exactly the same thing to what I use to. What’s the worst that can happen? And then when I run the code, I discover a countless number of bugs for reasons that I don’t understand. Sometimes, even worst: the whole architecture of my code is wrong because I didn't understand how JavaScript works and I need to start from scratch. (I still have yet some of these issues ☺). Through this series we are going to discuss the dark parts of JavaScript: the parts where I got most of the troubles. The Scope Global Variables A global variable can be accessed and modified outside and inside the function where it was defined. A variable which is declared outside a function with “var” is a global variable. var a; a = 1; alert(a); // a = 1 function test(){ alert(a); // a = 1 a = 5; } test(); alert(a); // a = 5 Warning!! A variable which is declared inside or even outside a function without using a “var” is also a global variable. function test(){ a = 5; } test(); alert(a); // a = 5 Local Variables A local variable can be accessed and modified only inside the function where it was defined. A variable which is declared inside a function with a “var” is a local variable. function test(){ var a = 5; } test(); alert(a); // ReferenceError: a is not defined. Function arguments are also local variables. function test(a){ alert(a); // a = 5 } test(5); alert(a); // ReferenceError: a is not defined. Local vs Global When there are two variables: one is local and the other one is global and both have the same name, the local variable hides the global variable. var a; a = 1; alert(a); // a = 1 function test(){ var a = 5; alert(a); // a = 5 a = 4; } test(); alert(a); // a = 1 Block Scope There is no bock scope in JavaScript. var a; a = 1; alert(a); // a = 1 function test(){ if(true) { var a = 5; } alert(a); // a = 5 a = 4; } test(); alert(a); // a = 1 Actually Conclusion The main conclusion of this article is that the JavaScript scope is different to other programming languages like Java/C++ that we are used to. So please be careful about that and try to understand all these rules to avoid later a lot of bugs. Original Post - https://medium.com/@riadh/javascript-the-dark-parts-scope-6172c90bff11 1 Like |
(1) (Reply)
Good News For News Lover World News Monitor / How Can I Integrate Biometric Verification Into A Site? / Mark Zuckerberg Will Launch "Dislike" Button On Facebook Very Soon
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 10 |