Home  >  Article  >  Web Front-end  >  Code implementation of js variable promotion

Code implementation of js variable promotion

不言
不言Original
2018-08-23 17:02:011730browse

The content of this article is about the code implementation of js variable promotion. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Ordinary variable declaration:

<script type="text/javascript">
    alert(a);             //报错  a is not defined
    var a = 1;
</script>

Variable promotion:

<script type="text/javascript">
	alert(a);          //undefined   if语句的变量提升
	if(1<2){
	    var a = 1;
	}
	box();
        function box(){
	    alert(b);      //undefined   函数的变量提升
	    if(1<2){
		var b = 2;
	    }
	}
</script>

Related recommendations:


The above is the detailed content of Code implementation of js variable promotion. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn