You add a semicolon ;
to mark the end of a statement. However, this isn't always enforced, and several js parsers will accept using just the line break as the end of the statement. This is currently seen as a bad practice, because in certain scenarios the parser has no way of discriminating coding linebreaks from literal linebreaks. For example, if you tried to declare a string containing a line break, you gonna have a bad time.
There are certain constructs that do not have a closing semicolon. Funcion
declarations (as opposed to function expression as in your example) do not carry a trailing semicolon:
function orangeCost(price) {
var quantity=5;
console.log(price*quantity);
}
You don't use trailing semicolons after loops such as if/else, for or switch either.
This isn't different from PHP, not at all. In PHP the same applies to procedural functions and lambda functions:
<?php
function orangeCost($price) {
$quantity=5;
echo $quantity*$price;
}
$orangeCost=function($price) {
$quantity=5;
echo $quantity*$price;
};
the only difference would be that in PHP the lambda function would have to be invoked using $orangeCost(5)
instead of orangeCost(5)
.