20 Getting started scope.html scope.js l conflict. without functions different in used be can name variable same the so – block function own their throughout unique be need only names variable local but script entire the throughout unique be must names variable variables.Global other with conflict accidental possible prevents scope limited their as variables global to preferable generally are variables Local • • declared: is variable the where by determined is its “scope” and called is accessible is variable a which to extent The Recognizing l l l 4 3 2 1
known asa “ withinanysois function inthatscript – itisaccessible globally code withinany function block. hasThis variable “global” scope known asa “ withinthat function, accessible locally scope –itisonly sois code withinthatsamefunction block. “local” hasThis variable A variable declared outside all function blocks isaccessible functionto outsideall blocks declared A variable accessible to isonly insideafunction block declared A variable In the functiontheIn block, declareinitializeandvariablelocal a loaded has document the after execute to function a Add a initialize and declare then editor text plain a Open Create a HTML document that embeds an external external an embeds that document HTML a Create var obj=document.getElementById(var “ window.onload=init; } { init() function var global=” var variable global
id=”
! JavaScript is Not Enabled. Not is JavaScript ! global variable” local variable” local panel le and has a “panel”a has element and fle This is Worldwide Global news
21 A variable can be declared without initialization, then assigned a value later in the script to initialize it. only visible within their respective function block. Notice that the “local” variable names do not conflict because they are
”;
”;
---This is European Local news--- Local is European ---This ***This is news*** Local States ***This United
global and local variables written by the functions obj.innerHTML+=local; obj.innerHTML+=global; } open the page in your browser to see the values of the one argument that initializes a local variable, then appends then appends a local variable, that initializes one argument into the its and panel value that of variable the global eu(obj) function { var local=” function us(obj) function { var local=” obj.innerHTML+=local; obj.innerHTML+=global; } us(obj); eu(obj); its then appends a local variable, that initializes argument into the and panel value that of variable the global variable into the panel obj.innerHTML=global; passing the value of the local variable to each one Save the script alongside the HTML document then Before the init function block, insert another function with function another insert block, the init function Before Now Now in the function block call two other functions, with one a insert function block, function “init” the Before Next Next in the function writeblock, the value of the global