Learning Ext JS 3

What is the scope of a variable? Más videos

Descripción del tema

When we talk about "scope", we are referring to the scope or the area where a variable can be used, in this chapter we will see how JavaScript handles this concept. In JavaScript the scope of the variables takes place in a function and not in blocks of code (if, while, switch, etc.) like in languages Java or C/C++; in other words if a variable gets defined inside of a conditional block (if) this variable can be used in all the function and not only in the conditional block. Let's see the next example:
if(true){
            var test = 'is it a block var?';
}

function testing(){
            var test = 'testing scope!';
}
testing();

console.debug(test);
Inside of the condition the variable "test" was defined, in languages like Java this variable would exists only inside of the condition, but in JavaScript the scope is different because the variable "test" was defined in the "global scope" and not inside of the conditional block. Furthermore the other variable that was defined inside of the "testing" function only exists within that function. It is important to remember that the variables that are declared in the "global scope" are properties of the object "window", to verify this assertion let's do the following:
var global = 'this is a global var!';

console.debug(window.global);
Another thing to consider is that when the variables are not declared using the keyword "var" it doesn't matter if they are inside of a function or not, these variables are automatically defined in the "global scope".
function globalScopeFunction(){
            globalScope = 'this is a new var in the global scope!';
}
globalScopeFunction();

console.debug(globalScope);
console.debug(window.globalScope);
It is important to know these concepts because we will use them later on, so now we know what is exactly the scope of the variables and where we can use it.

Te gustaría recibir más tutoriales como este en tu correo?

Este tutorial pertenece al curso Learning Ext JS 3, te recomiendo revises el resto de los tutoriales ya que están en secuencia de menor a mayor complejidad.

Si deseas recibir más tutoriales como este en tu correo te recomiendo registrarte al curso, si ya eres miembro solo identifícate y registrate al curso, si no eres miembro te puedes registrar gratuitamente!

Si no gustas registrarte en este momento no es necesario! Aún así puedes recibir los nuevos tutoriales en tu correo! Jamás te enviaremos Spam y puedes cancelar tu suscripción en cualquier momento.

¿Olvidaste tu contraseña?

Se el primero en comentar!

Instructor del curso

Crysfel3

Autor: Crysfel Villa

Software engineer with more than 7 years of experience in web development.

Descarga Código Fuente

Regístrate a este curso

Este tutorial pertenece al curso Learning Ext JS 3, revisa todos los tutoriales que tenemos en este mismo curso ya que están en secuencia y van de lo más sencillo a lo más complicado.

Tendrás acceso a descargar los videos, códigos y material adicional.

Podrás resolver los ejercicios incluidos en el curso así como los Quizzes.

Llevarás un registro de tu avance.