What is the difference between var functionName = function() {} and function functionName() {} in JavaScript?

Learn how the JavaScript engine loads function declarations and expressions differently, and how this affects your code.

By Jeremy McPeak
September 12, 2016
Screenshot from "What is the difference between var functionName = function() {} and function functionName() {}?" Screenshot from "What is the difference between var functionName = function() {} and function functionName() {}?"

While the end result may often prove the same, there is an important difference between writing a function as a declaration and as an expression. In this video, Jeremy McPeak will teach you how to make your functions available at any time using “hoisting,” or function declaration, and offers his take on best practices. Beginner and intermediate web developers will learn how the JavaScript engine loads function declarations and expressions differently, and how this affects their code.

Take your JavaScript skills to the next level with Learning Paths in Safari.

Learn faster. Dig deeper. See farther.

Join the O'Reilly online learning platform. Get a free trial today and find answers on the fly, or master something new and useful.

Learn more

Jeremy McPeak is a self-taught programmer who began his career tinkering with websites in 1998. He is the author of Beginning JavaScript, 5th Edition, JavaScript 24-Hour Trainer, and co-author of Professional Ajax, 2nd Edition. He also contributes to Tuts+ Code, providing articles, video tutorials, and courses on JavaScript, C#, ASP.NET, and PHP. He is currently employed by an oil and gas company, building in-house conventional and web applications.

Post topics: Web Programming
Post tags: Questions
Share: