Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript get value outside function

/*
Scope is important in JavaScript (and probably most
programming languages). If a variable is declared
within a local scope, it can only be accessed from
within that local scope.
*/
function localScope() {
	// this variable is declared locally, it can only
	// be accessed from within this function
	let localVariable = "something";
}
// if you were to try to access localVariable out here
console.log(localVariable); // ReferenceError: 'localVariable' is undefined
// you would not be accessing the variable you declared inside the function

// to get a specific value from a function, you can use
// the return keyword to return a specific value
function add(x, y) {
	return x + y;
}
const two = add(1, 1);
 
PREVIOUS NEXT
Tagged: #javascript #function
ADD COMMENT
Topic
Name
1+7 =