what does `_` mean in javascript? [duplicate] - javascript

This question already has answers here:
Javascript: what does function(_) mean
(3 answers)
Closed 5 years ago.
I came across the following code:
Array.apply(null, Array(5)).map(function (_, i) {return i;});
What does _ mean in the above code? I tried replacing _ with null but it is saying syntax error. However undefined would work. I am new to Javascript. So any comments would appreciated! Thanks!

It is just a regular variable. In some languages, like F#, it means that this argument is not intended to be used. For example, see F#'s underscore: why not just create a variable name?

Related

JavaScript object properties copy syntax [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
What do {curly braces} around javascript variable name mean [duplicate]
(1 answer)
Closed 4 years ago.
I was reading some JavaScript code and saw a line of code similar to this.
const {name, password, email} = user;
I tried searching around but could not figure out what this syntax is called so I am not able find the documentation. Is this a new JavaScript syntax? What is it called? What does it do exactly?

Javascript square brackets around method name [duplicate]

This question already has answers here:
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 5 years ago.
In the RxJs doc I found following code snippet:
[rxSubscriberSymbol]() {
return new SubjectSubscriber(this);
}
Its part of the Subject source code and is the first method right after the constructor.
So what do square brackets mean in this context?
those are symbols, which is very similar to defining properties but gives different accessibility and testability functionality and they are completely unique,
you can read a lot more about metaprogramming here,
Metaprogramming in ES6: Symbols and why they're awesome

What does exclamation point after variable mean in JavaScript? [duplicate]

This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Closed 5 years ago.
I've seen in some React/TypeScript implementations such as :
ref={ ref => this.container = ref! }
What does the exclamation point means in ref!?
Is that something specific in TypeScript, or a new standard JavaScript notation?
In TypeScript, a postfix ! removes null and undefined from the type of an expression.
This is useful when you know, for reasons outside TypeScript's inference ability, that a variable that "could" be null or undefined actually isn't.

Significance of defining custom methods to window object in javascript [duplicate]

This question already has answers here:
What's the difference between a global variable and a 'window.variable' in JavaScript?
(5 answers)
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 5 years ago.
I am working on a project in javascript and while going through design pattern concepts came across following point.
I have a sample code as below:
window.customfun = (function(){}())
Here i have two questions:
1) Can we not simply define a global function as below:
customfun = (function(){}())
2) (function(){}()) is an anonymous function with its own evaluation environment but i have learnt that to be implemented as : (funtion(){})()see the position of parentheses around couple of parentheses. I found that both are working so i wanted to know if there is any difference between them?
Sure, you can define global functions
No, there is actually no difference.
Is there a difference: (1+(1)) === (1) + (1) ?

Reversing a variable in javascript [duplicate]

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 8 years ago.
I don't really have a good way to explain in words what i wont to do.So im just going to have an example.
this is what the variable would be before.
var foo ="foo";
this is what i wont it be after.
var foo ="oof";
I hope that you under stand what i'm asking!
Thinks !
Try this:
var foo="start".split("").reverse().join("");

Categories