JavaScript semicolon after a function [duplicate] - javascript

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.

Related

Does return is added implicitly in a callback function? [duplicate]

This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
Closed 2 years ago.
I have a code (attached screenshot) that in one place it is written explictiy reutrn inside a callback, and in another one it isn't. I'm trying to understand what is the reason for it? In my opinion return statement should be added also in the first one. Am i wrong?
There is a return in the first one. Read this about arrow functions
"(...) If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword" - https://www.w3schools.com/js/js_arrow_function.asp
What you're seeing is a property of the arrow function: if the braces are omitted the result of the following statement is returned automatically.
input => output is the same as input => { return output; }.
Note that this behavior differs from regular functions, as these two functions do not both return a result:
function a(input) { return 1 };
function b(input) { 1 };
a() // 1
b() // undefined

Javascript filter function must be always written on one line? [duplicate]

This question already has answers here:
Curly Brackets in Arrow Functions
(3 answers)
Closed 2 years ago.
I spend some time figuring out what was the problem with this
arr.filter((order)=>{
order.trackingNumbers.some((track)=>{
track.number==search
})
})
and then noticed that it works if is written in just one line
arr.filter(order=>order.trackingNumbers.some(track=>track.number==search))
why is that and is posible to write it on several lines?
arr.filter((order)=>{
return order.trackingNumbers.some((track)=>{
return track.number==search
})
})
you can omit return keyword when you're omitting bracket thats why the single line works

parentheses before function syntax js meaning [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 2 years ago.
I came across with a piece of code with new syntax for me, What this syntax means in js? I mean this parentheses at the beginning:
(function () {
//...
})()
It's called IIFE
basically, you can define a a function and invoke it immediately without declaring a name for it.

Why do this returns a non expected result? [duplicate]

This question already has answers here:
Why is [1,2] + [3,4] = "1,23,4" in JavaScript?
(14 answers)
Closed 4 years ago.
So I'm learning about the reduce method and I write this code...
function getSum(x,y){
return x+y
}
var arraySum = function(array) {
return array.reduce(getSum)
};
arraySum([1,[2,3],[[4]],5])
But it actually returns a string of the elements all-together...
I'm actually trying to sum them all... I expected the result to be 15 instead of "12,345"
What's happening? what am I doing wrong?
Your problem is you're adding actual arrays together, not the content within the arrays. Add a counter, and an incrementational variable like i inside the variable arraySum, and call the position of i, incrementing it every time, it should fix your problem.

How to create a repeatString Function [duplicate]

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);
}

Categories