views:

1241

answers:

6

i simply love javascript ... it's so elegant (imagine the quiet sound of lovestruck fanboy sighing in the background).

so, recently i have played with Lua via the löve2d framework (nice!) - and i think Lua is also great. they way i see it, those two languages are very similar.

there are obvious differences, like

  • syntax
  • problem domain
  • libraries
  • types (a bit)

but which are the more subtle ones? is there anything a javascript coder would take for granted that works in Lua just slightly different? are there any pitfalls that may not be obvious to the experienced coder of one language trying the other one?

for example: in Lua, arrays and hashes are not separate (there are only tables) - in javascript, they are numerical Arrays and hashed Objects. well, this is one of the more obvious differences.

but are there differences in variable scope, immutability or something like this?

+4  A: 

To be honest it would be easier to list the things which are common to Javascript and Lua than to list the differences. They are both dynamically-typed scripting languages, but that's about as far as you can go really. They have totally different syntax, different original design goals, different modes of operation (Lua is always compiled to bytecode and run on the Lua VM, Javascript varies), the list goes on and on.

Dave Rigby
absolutely. the very different goals include a high priority for having a clean language. Javascript has a lot of historical baggage, Lua continually sheds anything that's undesired.
Javier
+1. I don't even see how they're similar at all, except for the fact that they're both used for scripting (which is too obvious).
musicfreak
+1  A: 

Off the top of my head

Lua ...

  1. supports coroutines
  2. has no restriction to just string/number as key for a table. Everything works.
  3. the error handling is somewhat clumsy. Either you don't handle anything or use the pcall method
  4. I think I read something about differences in the lexical scope and that Lua has the better one.
  5. If I recall correctly regular expression support in lua is limited
jitter
Lua _does_ have lexical scope. JavaScript only has function scope. well, in Mozilla and Rhino yo can now use 'let' instead of 'var' and get proper lexical scope; but it's not portable yet.
Javier
Lua's standard string library includes limited pattern matching functions; but there's also LPEG (also a library), which gives a much more powerful matching system, easily usable for a full grammar.
Javier
I stated that LUA has the "better" lexical scope then javascript not that it hasn't any.
jitter
LPEG is an additional library which means core regex support is limited to me
jitter
+21  A: 

Some more differences:

  • Lua has native support for coroutines.
  • Lua doesn't convert between types for any comparison operators. In JS, only '===' and '!==' don't type juggle.
  • Lua has an exponentiation operator (^); JS doesn't. JS has many more operators, including the ternary conditional operator (?:), increment/decrement, bitwise operators, type operators (typeof and instanceof), additional assignment operators and additional comparison operators.
  • In JS, the equals and not equals operators are of lower precedence than less than et al. In Lua, all comparison operators are the same precedence.
  • Lua supports tail calls.
  • Lua supports assignment to a list of variables.
  • In Lua, you can overload operators.
  • In Lua, you can manipulate environments with getfenv & setfenv.
  • In JS, all functions are variadic. In Lua, functions must be explicitly declared as variadic.
  • JS has global and function scope. Lua has global and block scope.
  • Foreach in JS loops over object properties. Foreach in Lua loops over iterators and is more general.
  • Integer literals in JS can be in octal.
  • JS has explicit Unicode support.
  • In lua, ~ is used in place of !. (as in, if foo ~= 20 then ... end) (technically syntax but it's a subtle point that still catches me occasionally sometimes)
  • In lua, the not/or/and keywords are used in place of !/||/&&. (also syntax but sometimes forget about this too)
outis
in Lua, logical operators (and, or) do return one of the arguments. all functions can be called with any number of parameters; but are adjusted to the needed number (unless you use the ... 'extra args')
Javier
thanks, that's a very nice list (and exactly what i hoped for). "# In Lua, logical operators return boolean values. In JS logical operators return the value of one of their arguments." you switched those, as javier noticed.
Schnalle
Whoops. Corrected false statement about logical operators. The statement about ! in Lua threw me. That'll teach me to read too fast. Community wiki, so keep adding everyone.
outis
A default Lua installation has limitations (usually 250-256) on numbers of locals/globals that can be declared in a program.
SztupY
Since when? i don't recall that... Perhaps a memory limitation on whatever you were testing on?
RCIX
A: 

JavaScript arrays and objects are closer than you might think. You can use array notation to get at the elements of either of them, and you can add non-numeric indices to arrays. Individual array elements can hold anything, and the array can be sparse. They are nearly identical cousins.

Nosredna
+2  A: 
rq
A: 

Lua and JavaScript are both prototype base languages.