ReferenceError does not occurs? [duplicate] - javascript

This question already has answers here:
Why is no ReferenceError being thrown if a variable is used before it’s declared?
(3 answers)
does this variable get hoisted no matter what?
(1 answer)
Closed 5 years ago.
I have a question about following code blocks
var x = "123" + y;
in this code block I am getting
Uncaught ReferenceError: y is not defined
but when I try something like this
var x = "123" + y;
var y = "456";
It works fine, so why? What is the difference?

Related

I got an Uncaught Type Error when I tried to use IIFE's [duplicate]

This question already has answers here:
What are the rules for JavaScript's automatic semicolon insertion (ASI)?
(7 answers)
Closed 5 days ago.
I am new to Javascript and I'm trying to understand what IIFEs are. I saw the example below on youtube it worked well on the video but when I tried to do the same thing I got an error like
Uncaught TypeError: 4 is not a function
the code:
var a = 4
(function(){
var a = 5
console.log(a)
})()
console.log(a)
I searched google but I couldn't find an answer.
just add ; between 4 and ( for example:
var a = 4; // <---- add to this
(function(){
var a = 5
console.log(a)
})()
console.log(a)
if you don't have it the browser will remove that space and become 4(...) i.e. you are calling a function called 4 with a function parameter

output of the javascript snippet? javascript variable declarations [duplicate]

This question already has answers here:
How JS hoisting works within functions?
(4 answers)
Why a variable defined global is undefined? [duplicate]
(3 answers)
Closed last year.
My first guess is that the following code has the output of 10 and 100. However, after running the code I get undefined and 100 and I don't understand why the function doesn't see the first declaration of x..
var x = 10;
function f() {
console.log(x);
var x = 100;
console.log(x);
}
f();

Redeclared variable inside the block using let keyword and accessed before it but gives error [duplicate]

This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
What is the temporal dead zone?
(3 answers)
Closed 2 years ago.
var a=10;
{
console.log(a);
let a =20 ;
}
ReferenceError: a is not defined
Why it is giving the reference error even a is declared using var which should have its scope inside the block too ?

Why this give Uncaught ReferenceError [duplicate]

This question already has answers here:
What is the temporal dead zone?
(3 answers)
Closed 3 years ago.
the expected output as far as I understand must be :
10
22
let y = 10;
{
console.log(y); // this line gives refrence error
let y = 22;
console.log(y);
}
{
console.log(this.y); // this line gives refrence error
let y = 22;
console.log(y);
}
You are trying to call a variable that is not declared yet. Thats why you're getting this error. Your first console.log is not looking at the global scope ,it's looking at the block scope. And at the time you're calling it, it's not defined yet. A " var" would've returned undefined. let and const return this error. I hope it's clear.
You can test this yourself using 'this'. as you can see this.y is undefined.

javaScript: Why does toString() not work directly with a number but with a number variable declared? [duplicate]

This question already has answers here:
number toString() method returns different values for number and variable [duplicate]
(4 answers)
Closed 4 years ago.
y = 15;
let x = y.toString(10);
console.log(x)
This prints '15' but
let x = 15.toString(10);
console.log(x)
gives Error: Invalid or unexpected token
Why?
It works with another dot for separating the property from number.
let x = 15..toString(10);
console.log(x);

Categories

Resources