5 JavaScript concepts that every developer should know
JavaScript is one of the most popular programming languages used primarily in web development. Today, I’ll share five concepts that I believe every web developer should be familiar with.
1. Hoisting
Hoisting in JavaScript is a behavior that moves declarations of classes, functions, variables, or imports to the top of their respective scopes (the top of the current script or function).
Variables:
Let’s see how hoisting impacts variables:
console.log(name) // undefined
var name;
As you can see, JavaScript allows us to reference the variable name
before it's declared. This is because variable declarations are hoisted to the top of their respective scopes, giving them a default value of undefined
console.log(name) // ReferenceError: Cannot access 'name' before initialization
let name;
console.log(name) // ReferenceError: Cannot access 'name' before initialization
const name = 'John';
With let
and const
, the behavior is quite different. Since neither has a default value, referencing them before declaration results in a ReferenceError
.
There’s an interesting behavior in the JavaScript interpreter.