Variable hoisting

Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted to the top of the function or statement. However, variables that are hoisted will return a value of undefined. So even if you declare and initialize after you use or refer to this variable, it will still return undefined.

/**
* Example 1
*/
console.log(x === undefined); // true
var x = 3; /**
* Example 2
*/
// will return a value of undefined
var myvar = "my value"; (function() {
console.log(myvar); // undefined
var myvar = "local value";
})();

The above examples will be interpreted the same as:

/**
* Example 1
*/
var x;
console.log(x === undefined); // true
x = 3; /**
* Example 2
*/
var myvar = "my value"; (function() {
var myvar;
console.log(myvar); // undefined
myvar = "local value";
})();

Because of hoisting, all var statements in a function should be placed as near to the top of the function as possible. This best practice increases the clarity of the code.

In ECMAScript 2015, let (const) will not hoist the variable to the top of the block. However, referencing the variable in the block before the variable declaration results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.

console.log(x); // ReferenceError
let x = 3;

Function hoisting

For functions, only function declaration gets hoisted to the top and not the function expression.

/* Function declaration */

foo(); // "bar"

function foo() {
console.log("bar");
} /* Function expression */ baz(); // TypeError: baz is not a function var baz = function() {
console.log("bar2");
};

Variable hoisting Function hoisting的更多相关文章

  1. [JS] Topic - variable and function hoisting

    Ref: 深入理解js的变量提升和函数提升 一.变量提升 简直就是es5的遗毒! console.log(global); // undefined 竟然能打印?因为变量提升,下一行就有定义 var ...

  2. learning scala How To Create Variable Argument Function - varargs :_ *

    Scala collection such as List or Sequence or even an Array to variable argument function using the s ...

  3. CMake语法—普通变量与函数(Normal Variable And Function)

    目录 CMake语法-普通变量与函数(Normal Variable And Function) 1 CMake普通变量与函数示例 1.1 CMakeLists.txt 1.2 执行CMake配置脚本 ...

  4. Linear regression with one variable - Cost function

    摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第二章<单变量线性回归>中第7课时<代价函数>的视频原文字幕.为本人在视频学习过程中逐字逐句记录下 ...

  5. Linear regression with one variable - Cost function intuition I

    摘要: 本文是吴恩达 (Andrew Ng)老师<机器学习>课程,第二章<单变量线性回归>中第8课时<代价函数的直观认识 - 1>的视频原文字幕.为本人在视频学习过 ...

  6. [Code::Blocks] Install wxWidgets & openCV

    The open source, cross platform, free C++ IDE. Code::Blocks is a free C++ IDE built to meet the most ...

  7. 本人SW知识体系导航 - Programming menu

    将感悟心得记于此,重启程序员模式. js, py, c++, java, php 融汇之全栈系列 [Full-stack] 快速上手开发 - React [Full-stack] 状态管理技巧 - R ...

  8. [JS] Topic - why "strict mode" here

    Ref: Javascript 严格模式详解 使得Javascript在更严格的条件下运行: - 消除Javascript语法的一些不合理.不严谨之处,减少一些怪异行为; - 消除代码运行的一些不安全 ...

  9. ES3之变量提升 ( hoisting )

    JavaScript引擎在预编译时,会将声明(函数声明.变量声明)自动提升至函数或全局代码的顶部.但是赋值不会提升. Because variable declarations (and declar ...

随机推荐

  1. HAWQ取代传统数仓实践(十二)——维度表技术之分段维度

    一.分段维度简介 在客户维度中,最具有分析价值的属性就是各种分类,这些属性的变化范围比较大.对某个个体客户来说,可能的分类属性包括:性别.年龄.民族.职业.收入和状态,例如,新客户.活跃客户.不活跃客 ...

  2. Windows下修改hosts并且让他立即生效

    1.打开hosts所在的目录 Win+R->C:\windows\System32\drivers\etc 2.编辑hosts文件 使用Notepad++或者记事本以管理员身份打开hosts,修 ...

  3. BZOJ - 1941 Hide and Seek (kd树)

    题目链接 kd树模板题,求二维空间上的最远点/最近点. 对所有点建立kd树,分别查询每个点即可.单次查询期望时间复杂度$O(logn)$ #include<bits/stdc++.h> u ...

  4. Microsoft Office Visio 2013 (安装 + 激活)

    Visio是一款能处理复杂信息.系统和流程进行可视化.分析和交流的软件,从“office 2003”以后,Visio作为一个单独软件发行,不再集成于office办公软件. 工具/原料 Visio 电脑 ...

  5. codevs4189字典

    沙茶 题目大意:求某一个字符串前缀有没有在n个字符串前缀里出现过 题解:Trie树 查询前缀有没有出现 代码: //codevs4189 #include<iostream> #inclu ...

  6. Linux环境下安装jenkins

    废话不多说,直接开始 1.从官网下载Jenkins的war包 2.下载好的War放到Tomcat的网站根目录webapps下,然后启动Tomcat. 3.打开浏览器,输入http://IP:8080/ ...

  7. 洛谷 2680 (NOIp2015) 运输计划

    题目:https://www.luogu.org/problemnew/show/P2680 因为是最长的时间最短,所以二分! 离线LCA可以知道路径长度.每次只看超过二分值的路径. 原本的想法是遍历 ...

  8. (装)Android杂谈--禁止TimePicker控件通过keyboard输入

    Android 4.1版本以上用的是类似与ios的滚动时间控件,但是4.1以下,用的TimePicker确实通过点击上下按钮来更改时间的,虽然也提供了编辑框编辑,但是可能会超出编辑范围 如果要禁止编辑 ...

  9. Java Integer和String内存存储

    标签: java内存string 2016-01-10 12:51 1545人阅读 评论(2) 收藏 举报  分类: Java(7)  版权声明:本文为博主原创文章,未经博主允许不得转载. 先看代码: ...

  10. EasyWeChat微信开放平台第三方平台接入

    EasyWeChat微信开放平台第三方平台接入 https://www.cnblogs.com/bainiu/p/8022729.html