소스 코드를 기록하는 남자

'Global'에 해당되는 글 2건

  1. JavaScript - Global Variable
  2. JavaScript - Hoisting

JavaScript - Global Variable

JavaScript

In javaScript, it is different from other langauge.

 

Lets see the code.

 

after showAge function called, the variable "age" is changed to a Global Variable 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">

	// Global Variable Declaration (3) a Global variable connected to window object
/* 	var myName = "James";
	//
	firstName = "Gosling";
	//
	var name;
	name = "James Gosling";
	
	console.log(myName +' '+firstName+" "+name); */
	
	//2. A Global Variable with function
	
	function showAge() {
		age = 90;
		//console.log(age);
	}
	showAge(); //after function called, age will be a global variable
	console.log(age);
	
	
</script>
</head>
<body>

</body>
</html>

'JavaScript' 카테고리의 다른 글

JavaScript - typeof(), prompt(), String(), Number()  (0) 2019.09.14
JavaScript - Function Executing Process  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14
JavaScript - Escape character, Operator  (0) 2019.09.14

JavaScript - Hoisting

JavaScript

In JavaScript, there is a rule for Declaration.

 

Every variable will be hoisted to top. 

 

Remember! Just Declaration, Not a value

 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>호이스팅 :: Hoisting</title>
<script type="text/javascript">
//1. hoisting
 	doSomething();
	
	function doSomething() { 
		alert("not defined : "+some); //undefined
	
		var some = "WellBeing";
		
		alert("defined : "+some); //WellBeing
	}; 
//2. Global Variable
var name = "James";
function showName(){
	var name = "Gosling";
	console.log(name);
}
console.log(name);
showName();
</script>
</head>
<body>

</body>
</html>

'JavaScript' 카테고리의 다른 글

JavaScript - Function Executing Process  (0) 2019.09.14
JavaScript - Global Variable  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14
JavaScript - Escape character, Operator  (0) 2019.09.14
JavaScript - Datatype  (0) 2019.09.14