Click here to Skip to main content
Click here to Skip to main content

Tagged as

Javascript Facts for great Javascript coding - Part 1

, 27 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Introduction Java script is an interesting and popular programming language. Its syntax is easy, features are cool and its role is becoming more and more important every day in web development.



Java script is an interesting and popular programming language. Its syntax is easy, features are cool and its role is becoming more and more important every day in web development. Usually it is easy to get started with JavaScript because a lot of resources and community help is available. Starting this way works but fundamentally there are a few thing in Java Script which are different from other programming languages. I m not saying this post is covering everything need to do great Java Script development but I m sure knowing and and following these facts will help you doing great Java Script coding. Knowing basics of Java Script syntax is good to have to gain maximum benefits from this article.

1. Variable scope in Javascript is different

In C# or Java variable scope has limit inside curly braces, it means a variable declared inside “{}” cannot be accessed out of “{}”. It is not the case in Java Script and a variable declared inside “{}” is not private and is accessible out of block as well.

if (true) {
var no = 50; //variable declared and value initilized
console.log("Variable value inside block: " + no);
console.log("Variable value outside block: " + no); // accessable

2. There are only two types of variable scopes in JS Global and Local A Java Script variable can be public or private. A variable declared inside Java Script function is refered as private, means it cannot be accessed out of function but other wise a function is always global and can be accessed throughout script.

var var1 = 12;
//var1 is accessable here of course
(function SelfExecutedTestFun() {
console.log("value of globalNo from self executed function is: " + var1);
var var2 = 500;
console.log("value of var1 from out side self executed function is: " + var1);
console.log("value of var2 from out side function is: " + localno); // error var2 is not defiened

3. JS is a hoistic language, so it allows using a variable in code and declaring it later.

no = 50;
var no;

4. A JS variable can be used without declaration, and its scope will be Global

no = 50;

5. JS allow doing a few thing which looks funny. For example we can use a variable without declaring, we can delete a variable, can declare a variable multiple times, having multiple function parameters of same name and using a few future reserved key words like public, private, interface etc. So all below case are ok in JS.

no = 50;
delete no;
console.log("accessing no after deleting it: " + no);
var no = 60;
var no;
console.log("declaring a variable multiple times and accessing it" + no);
function TestFun(para1, para1) {


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Australia Australia
Follow on   Twitter

Comments and Discussions

GeneralMy vote of 1 Pinmemberarchippus15-Oct-14 4:08 
GeneralMy vote of 1 Pinmemberyvsmrds2-Sep-14 10:00 
GeneralMy vote of 1 Pinmemberbeantaxi2-Sep-14 7:59 
GeneralMy vote of 1 PinprotectorRichard MacCutchan1-Sep-14 1:14 
QuestionSelf or Immediate? Pinmemberarchippus28-Aug-14 19:36 
QuestionNot exactly best practice Pinmemberestump27-Aug-14 6:07 
QuestionJavascript not Java Script PinprotectorRichard MacCutchan27-Aug-14 4:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150331.1 | Last Updated 27 Aug 2014
Article Copyright 2014 by YawerIqbal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid