Unexpected hehaviour when defining vairables [duplicate] - javascript

This question already has answers here:
JavaScript 'hoisting' [duplicate]
(5 answers)
Javascript function scoping and hoisting
(18 answers)
What are the precise semantics of block-level functions in ES6?
(2 answers)
Closed 1 year ago.
I have come across a very strange JavaScript code. Its execution result is completely different from what I expected. I want to know why.
var  a  =  1999;
{
console.log(a);
a  =  2020;
function  a()  {}
a  =  2021;
}
console.log(a); //Why 2020?

Related

Does A Single function accessing global variable can make closure? [duplicate]

This question already has answers here:
Is it true that every function in JavaScript is a closure?
(2 answers)
JavaScript closures vs. anonymous functions
(12 answers)
Will a simple function declaration form a closure in JavaScript?
(4 answers)
When closures are created in JavaScript
(4 answers)
Closed 10 months ago.
const a = 'MDNS';
function init() {
console.log(a)
}
init();
Is the above code is a closure?
or Is there any other way we can make closure from single function only.

Variable value not gets updated if function has same name as variable (JavaScript) [duplicate]

This question already has answers here:
Why can variable declarations always overwrite function declarations?
(3 answers)
javascript hoisting: what would be hoisted first — variable or function?
(2 answers)
Closed 2 years ago.
I expected the answer to be "function text" as output, why the answer is 5?
var alpha = 5;
function alpha(){}
console.log(alpha);

Why does. this arrow function not return an error? [duplicate]

This question already has answers here:
Value returned by the assignment
(5 answers)
Declaring variables without var keyword
(8 answers)
Closed 2 years ago.
In this function the cm variable has no data type eg var const or let. How does it still work without giving me an error?
const inchToCm = inch => cm = inch * 2.54;
Is it because it used an implicit return?

Number functions and other functions issue [duplicate]

This question already has answers here:
Calling the toFixed method on a number literal [duplicate]
(3 answers)
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 5 years ago.
Can someone logicaly explain why fallowing function return error
50.toFixed(2);
and this not
(50).toFixed(2);
Same happening with other similar number functions

Calling function using a pair with first element '0' and second element the function itself [duplicate]

This question already has answers here:
Calling function with window scope explanation (0, function(){})()
(2 answers)
JavaScript syntax (0, fn)(args)
(2 answers)
(1, eval)('this') vs eval('this') in JavaScript?
(4 answers)
What is the meaning of (0, someFunction)() in javascript [duplicate]
(3 answers)
What is the purpose of calling (0, func)() in JS? [duplicate]
(1 answer)
Closed 2 years ago.
I've seen that code in open source projects and I wonder what is its intent:
'use strict';
function invariant(condition, message) {
if (!condition) {
throw new Error(message);
}
}
(0, invariant)(false, 'failed');
Is this done to disable assertions? If it is, then I was unable to figure out how.

Categories

Resources