Skip to content

Instantly share code, notes, and snippets.

@AnastasiaDunbar
Last active December 17, 2018 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnastasiaDunbar/d56a44adee86afbc5a61789dfb4cb734 to your computer and use it in GitHub Desktop.
Save AnastasiaDunbar/d56a44adee86afbc5a61789dfb4cb734 to your computer and use it in GitHub Desktop.

JavaScript

Automatic semicolon insertion

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.

parseInt() on scientific notation (and the octal numeral system)

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.

Restricted regular expressions

No conditionals ((?(?=cond)|then|else)) or negative lookbehinds.

Immutable strings

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;

Lua

No ternary operator

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

1-based indexing

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.

Words for code blocks and operators

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.

No shorthand assignment operators

Instead of x += y you do x = x + y in Lua.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment