Places where can I declare variables in JavaScript [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
I am new to JS. I have the following question -
Is it right to say that I can declare variables only in global scope or inside a function/method only in JS and in no other type of Object which is not function/method?
I understand that properties can be defined in any object of Javascript but variables cannot be declared everywhere.
EDIT: I might not have right terminology to explain but in simplest words I need a list of places where i can write var x = .. in JS code. Please,I am not looking for scope of a variable.

you can the variable anywhere you want at the beggining of the program in the middle or if you need one in a function you can do it there too

Variables can be declared at top-level, function parameter lists, and statements in function bodies.
Object and array literals can contain nested functions, and you can declare variables inside these functions as well. But you can't declare variables directly in object/array literals; e.g. you can't write:
const foo_array = [
let a = 1,
let b = 2
];

Related

Which has better performance?Accesing global variable directly from a function or assigning it to another variable? [duplicate]

This question already has answers here:
How do you performance test JavaScript code?
(24 answers)
Closed 2 years ago.
Here I have two examples of accessing global variables;
basic example of my global variable
globalVar:any={
a:5,
b:3,
c:4
}
Ex.1
function accesVar(){
return this.globalVar.a*=2
}
Ex.2
function accesVar(){
let _var=this.globalVar.a;
return _var*=2
}
In this example global variable doesnt seem a good practise and I dont use them when I dont need to share variables with more than one function.Considering in general and real world(much bigger data),which would be best approach and has better performance?
Both have different importance.
Example one is direct use of globle variable. Saves you extra declaration.
Example two is useful when we need to use the global value many times inside the function.

Javascript ES6+, using bracket notation to access module global variables [duplicate]

This question already has answers here:
In what scope are module variables stored in node.js?
(4 answers)
Closed 4 years ago.
Is it possible to access a global variable inside an es6 module using the bracket notation, basically doing something like this:
const GLOBAL = 'something'
console.log(this['GLOBAL']) // evaluates to undefined, of course
Doesn't seem to be working
Global constants do not become part of the widnow object, unlike var.
This declaration creates a constant whose scope can be either global or local to the block in which it is declared, but if left alone in the global, they do not become property of the window object.

I want to know why we use var for all types of data [duplicate]

This question already has answers here:
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Closed 6 years ago.
What is difference between declaring variable using var and without var?
a=2;
b=2;
if(a==b)//returning false
if(a===b)//returning false
var a=2;
var b=2;
if(a==b)//returning true
if(a===b)//returning true
Why?
Without var will declare the variable globally. Using var will declare the variable locally in the current scope.
var defines variable globally, Means that variable defined with var can be accessible form any script below it. And without var it cant
We use var for all types because since JavaScript has dynamic typing the type can be inferred automatically during run-time.
Now, for the difference between using the var or not, if you're in a function then var will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope at which point it will create it.

JS: can a var/object declared as const be deleted [duplicate]

This question already has answers here:
delete or override const variables in javascript Harmony / ECMAScript 6
(2 answers)
Closed 7 years ago.
Newbie question:
Can const objects be deleted or overwritten in js?
const a = function myFunc(){}; // some code to delete 'a' and 'myFunc()' here.
A downside to using const is that you cannot clear the variable at will (const prevents it from being changed in any way).
If the variable is not in the global scope, then the garbage collector will take care of it when it goes out of scope. If it is in the global scope, then you cannot get rid of it. The work-around is to stop using const for variables that you wish to modify.
Properties of an object, assigned to a const variable are not affected by the const declaration as the const delcaration affects the variable, not the the contents that it points to. You can use Object.freeze() or Object.seal() if you want to affect properties of an object.

Javascript use variable name to thread a function? [duplicate]

This question already has answers here:
Use JavaScript variable as function name?
(5 answers)
Closed 7 years ago.
In GSC, you are able to make a variable become the name of a function that you thread. It looks like this:
variable = "pizza";
[[variable]]();
the engine then reads it like:
pizza();
my question is, is it possible to do that in javascript as easily or do I have to make if/else/switch statements for it?
my question is, is it possible to do that in javascript as easily or
do I have to make if/else/switch statements for it?
If you want to use the safe, fail-proof way, then you can access such variables only in two contexts.
If the variable is in global context, in the case of which, you can do window[variable]();
Else if the variable is a property of an object, in the case of which, you can do obj_name[variable](), basically anything that can be accessed via bracket notation. window is an object too.
Then there's always the dirty way:
You can use highly evil eval like eval(variable + "()") or you can use the Function constructor in the same way. Note however that both the methods can be misused and are highly advised against.

Categories

Resources