Javascript Facts

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.

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. 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;
console.log(no);
var no;
4. A JS variable can be used without declaration, and its scope will be Global
no = 50;
console.log(no);
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;
console.log(no);
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) {
}

No comments:

Post a Comment

Genuine websites to earn money.

If you are interested in PTC sites then this article is for you. I have personally tried many of the sites and found that the best thing ...