Hoisting

Variables declared with let and const are also hoisted but, unlike var , are not initialized with a default value. An exception will be thrown if a variable declared with let or const is read before it is initialized.Hoisting

but this statement from MDN Documentation isnt completely true is it

[codebyte language=javascript]
let num; // Declaration without initialization
console.log(num);//prints undefined instead of ReferenceError as stated above
[/codebyte]

Or am I missing something ?
Please help

Hi,
in your example you read num AFTER you initialized it. If you swap initialization and the console.log order, you’ll get your reference error:

console.log(num); // throws reference error as described in MDN docs
let num;

Whereas this doesn’t:

console.log(num); // prints undefined
var num;

Hello mirija_t
No I declared it not initialize it or isn’t that what the middle lines mean var Hoisting

console.log(num); // Returns 'undefined' from hoisted var declaration (not 6)
var num; // Declaration
num = 6; // Initialization
console.log(num); // Returns 6 after the line with initialization is executed.

Yes, you are right, I should have written ‘declared’ rather than ‘initialized’.
But nevertheless the example shows the difference of initialization between var and let/const:
Initialization is done by Javascript, it’s nothing you do. When you declare a variable with var, but without assigning a value, it is initialized at the very top of the scope as ‘undefined’. If you declare a variable with let, but without assigning a value, it is initialized as undefined where you declared it – not at the top of the scope.

var num; // Declaration
num = 6; // Assignment
let num; // Declaration and Initialization
num = 6; // Assignment

so if I understand what you are saying, initialization isn’t when you initially set a value to a declared variable (therein lies my confusion)
which is to say every time you declare a variable it is automatically initialized?

Yes, what you mean – setting a value – is assignment.

let num; // declaration
num = 1; // assignment
let num2 = 2; // declaration and assignment

Variables declared with let and const are also hoisted but, unlike var , are not initialized with a default value

actually I think this post says different unless I am misunderstanding the whole thing
just noticed itHoisting

I did not know that hoisting applies for let and const, too. So, I don’t know what hoisting does then for those two.
Where hoisting becomes relevant are these examples:

fn('hello'); // prints hello
function fn(str) {
 console.log(str)
}
fn('hello'); // throws a reference error
const fn = str => {
	console.log(str)
}