I embrace semicolons myself and it's more readable too. But the automatic semicolon insertion just might be useful in code golfing scenarios? In this example:
return
something;
will be converted into:
return;
something;
that it'll return undefined
.
Be aware that it converts the first parameter into a string:
parseInt(999999999999999934464); //Number gets converted into a string, in this case: "1e+21". So this will output `1`.
parseInt(0.000000999999999999999); //As a string: "9.99999999999999e-7". So this will output `9`.
parseInt("08"); /* or */ parseInt("09"); //The leading zero indicates an octal integer (base-8) that it outputs `0`, but this is changed in ECMA-262 so they'd output `8` and `9` as expected.
So use Math.floor(x)
or x|0
to convert your number into an integer.
No conditionals ((?(?=cond)|then|else)
) or negative lookbehinds.
Wanna replace a character at a specific index in a string? Good luck with that because strings in JavaScript are immutable. This is what you'd do:
(string,index,replacement)=>string.substr(0,index)+replacement+string.substr(index+replacement.length);
Instead of this:
string[index]=replacement;
We would use these workarounds instead but they come with problems:
--Be aware that if `ifTrue` is false then `ifFalse` will only be returned.
cond and ifTrue or ifFalse
function ternary(c,t,f)if c then return t else return f end end
--`something=cond?firstFunction(A):secondFunction(B)` would work completely fine in e.g. JavaScript. It skips evaluation.
--But functional-if won't work in this scenario:
something=ternary(cond,firstFunction(A),secondFunction(B)) --Because both functions will be called, but it returns correctly.
--To get around this:
if(cond)then
something=firstFunction(A)
else
something=secondFunction(B)
end
--Or:
something=ternary(cond,firstFunction,secondFunction)(ternary(cond,A,B))
As said in the manual, it is customary in Lua to start arrays with one (and not with zero, as in C) and the standard libraries stick to this convention. So I always have to do x-1
.
I'm more used to C-syntax with those curly braces and symbols rather than using words like end
, then
, and
, or
, not
and etc. for conditional statements and operators.
Instead of x += y
you do x = x + y
in Lua.