Skip to content
Akin C edited this page Apr 15, 2018 · 8 revisions

Welcome to the Javascript-global-variables-ex-var- wiki!

This repository is part of a larger project!

◀️ PREVIOUS PROJECT| NEXT PROJECT ▶️


In Javascript it is possible to declare global variables without the keyword var. It is pointed out that global variables should be used with caution, due to unexpected behaviour it can cause.

A declaration could look like as following:

//Instead "var number = 10;"
number = 10;

Interestingly global objects like JSON can be overwritten. It is recommended not to overwrite, because these kind of objects often provide better performance than an own implementation.

A pitfall of global variables in Javascript is that they can be declared unintentionally. For example a variable declared without keyword var within a function turns automatically into a global variable.

Content

Starting "index.html" in a web browser should show a content as seen below.

ERROR: No image found!

Short Explanation:

  • Item "Global" is the output of a global variable which was declared without keyword var

  • Item "Overwrite JSON" overwrites the JSON object and shows the result with the "BREAK" button

  • Item "Local as Global" shows the result of a globally declared variable within a function

The colored areas are just for a better readability in the wiki and are not part of the content. To use the project just download the files and execute "index.html". Note that all files should be placed in the same folder so that the functionality of the code is guaranteed.

Clone this wiki locally