— by Abhisek Pattnaik <[email protected]>
Last active
May 10, 2018 04:30
-
-
Save abhisekp/80ef2b34ca0008778c303b25a6fe788d to your computer and use it in GitHub Desktop.
Variable Declarations in JavaScript — http://bit.ly/js-vars
-
function param
implicit local variable declarationfunction sum(num) { // `num` is a local variable }
-
using
var
keyword
explicit local variable declaration inside a functionfunction getMe() { var answer = 'no'; }
or implicit global variable declaration when declared out of any function context
var age = 150; // global
-
using
const
keyword (similar tolet
)
implicit global variable declaration when declared out of any function contextconst name = 'Abhisek';
-
without using
var
,const
orlet
keywords (JS being overfriendly)
implicitly creates a global variable (bad!!!)⚠️ (doesn't work in strict mode) 👿function me() { answer = 'Abhisek'; } me(); console.log(answer) //=> 'Abhisek'
-
using
let
keyword
explicit local variable declaration inside a functionfunction person() { let age = 123; }
or implicit global variable declaration when declared out of any function context
let gender = 'male';
-
assigning as a property to global object e.g.
window
object
explicit global variable creationwindow.applicationName = 'TrimmerApp'; console.log(applicationName); //=> 'TrimmerApp'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment