This question already has answers here:
What's the difference between ++i and i++ in JavaScript [duplicate]
(6 answers)
javascript i++ vs ++i [duplicate]
(8 answers)
Closed 3 years ago.
I am trying to write a very simple script but do not understand why one syntax isn't working over the other.
The function is to simply increment any number by one.
This one does not work
function plusOne(x) {
return x++;
}
but this one does.
function plusOne(x) {
return x + 1;
}
What am I not understanding??
Increment (++)
If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing
You should use
function plusOne(x) {
return ++x;
}
Related
This question already has answers here:
Function with forEach returns undefined even with return statement
(5 answers)
What does `return` keyword mean inside `forEach` function? [duplicate]
(2 answers)
Can forEach in JavaScript make a return? [duplicate]
(6 answers)
Closed 1 year ago.
Here arr is array target is any number. I wants to return two numbers of arr. Sum of those number equals to value stored in target.
function twoSum(arr, target) {
var x= arr.forEach(e=>{
return arr.forEach(num=>{
if((e+num)===target){
return [e,num];
}
})
})
return x;
}
This question already has answers here:
setTimeout calls function immediately instead of after delay
(2 answers)
JavaScript setTimeout() won't wait to Execute? [duplicate]
(3 answers)
setTimeout in for-loop does not print consecutive values [duplicate]
(10 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 4 years ago.
can someone help me understanding why this code:
for (var i =0; i < 2; i++) {
setTimeout(console.log(i), 0);
}
console.log("aaa");
Will write:
0
1
aaa
But that code:
for (var i =0; i < 2; i++) {
setTimeout(function(){console.log(i)}, 0);
}
console.log("aaa");
Will wirte that:
aaa
2
2
Note that I understand how the second vers. work, I don't get why the first one make it differnt.
Thanks!
This question already has answers here:
Repeat String - Javascript [duplicate]
(30 answers)
How can I repeat strings in JavaScript? [duplicate]
(5 answers)
Closed 5 years ago.
so I am very new to js and I'm having a really hard time comprehending the language. Could anyone take 5 to look at this code and tell me why my function isn't working...? The goal is to create a function that duplicates a string by the number inputted into the function.
function repeatString(string, num) {
return string*num
}
var output=repeatString("Hello!",2);
console.log(output);
You could simply call the repeat method:
function repeatString(string, num) {
return string.repeat(num);
}
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 5 years ago.
for(i = 0; i < 10; i++){
setTimeOut(function(){
console.log(i);
},2000);
}
When I execute this, its printing 10, 10 times instead of 1,2,3....10. How do I fix this
You need to modify your code like this
function print(i){
console.log(i);
}
for(i=0;i<10;i++){
setTimeout(print.bind(null,i),2000);
}
This question already has answers here:
Why should I use a semicolon after every function in javascript?
(9 answers)
Do we need semicolon at the end? [duplicate]
(6 answers)
Closed 9 years ago.
I wonder the difference between two codes below in JavaScript, welcome answer.
Code 1:
function Add(a,b) {
return a + b;
}
Code 2:
function Add(a,b) {
return a + b;
};
The second code contains an empty statement. You can add as many as you want.
function Add(a,b) {
;;;;;
return a + b;;;;;;;
};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
First one is the standard method
Second one is not recommended.. Use the first method
Further reading - Why should I use a semicolon after every function in javascript?
There is no difference. The ; in the second example just creates an empty statement there.