/****************************
HOISTING
*/
//alert(a);
var a = 0;
/*****
is the same as
*/
var a;
//alert(a);
a = 0;
/***************************
SCOPE
*/
// functions also get hoisted
// i is globally defined because the keyword var is not used
function ab() {
for (i = 0; i < 10; i++) {
}
}
// i is defnied with var, making it function scoped
function ac() {
for (var i = 0; i < 10; i++) {
}
}
ab();
ac();
//alert(i);
function ad() {
//alert(i);
// var can only be function scoped
for (var i = 0; i < 10; i++) {
}
//alert(i);
}
ad();
// variables defined
function ae() {
var f = 0
return function () {
//alert(f);
//alert(blob);
}
}
var blob = 0;
function ag() {
//alert(blob);
// look for a blob var in the function scope, doesn't find it look in the upper scope (global in this situation), finds it and changes it...
blob = 1;
//alert(window.blob);
// ... If it didn't find it it would attach to window
blib = 2;
//alert(window.blib);
// Setting to global and/or window must be avoided
}
ag();
ae()();
// How to avoid variables collisions
// IIFE (Immediatelly invoked function expression)
// Safe blackbox that gets executed on page load...
(function(){
var private = 0;
// alert(private);
})();
// ... with no footprint
// alert(private)
/***********************
TYPES
*/
function ba(param1, param2){
param1.k = 1;
param2 = 1
}
// creates a reference in the memory to the location of the object
var bb = {
k:0
}
// assign a block in memory, and accessed directly
var bc = 0;
ba(bb, bc);
//alert(bb.k);
//alert(bc);
var bd = bb;
var be = bc;
//alert(bb === bd);
//alert(be === bc);
be = 3;
//alert(be === bc);
bd.k = 2;
//alert(bd === bb);
//alert(bb.k);
// Everything inherit from Object
var hello = "hello World";
//alert(hello);
//alert(typeof hello);
//alert(hello.length);
//console.log(hello.__proto__);
// String is just an extension of...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.