• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community
  • The forum is currently open to new registrations. The registration will close for a short period of time when reaching 500 active members, to upgrade the server resources.

Lua vs. JavaScript (1 Viewer)

Simon

Newcomer
Joined
Nov 19, 2020
Messages
6
Reaction score
0
Age
23
Location
Netherlands
How does Lua compare to other scripting languages such as JavaScript?
When would you use one over the other?
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
69
Reaction score
27
Location
Victoria BC
Website
winlua.net
There are lots of similarities, but on a whole the languages are quite different. Lua is a much more simple (I would say elegant) language in many respects.

Javascript is heavily used in webpages and web MVC frameworks like angular, as well as in web services running under nodeJS. Javascript can also be found in NoSQL databases like mongo.

Lua is a small scripting language that is very good for embedding in other applications. This is exemplified by it's use in the game industry. A game can be written to use Lua extensions and users can then create additional content. Roblox, Garry's Mod, World Of Warcraft are just a few names. Lua is also found in bootloaders (FreeBSD), routers (openwrt), and cellular modems (Sierra Wireless).

Both languages can be used as general scripting languages to an extent, however, both have different focuses. NodeJs is heavily web focused. It can be extended through the npm package manager. Lua is VERY small but it can be extended through the LuaRocks Package manager.

Javascript is a better language to learn if you want to do web stuff. Period. If you're young and you want to learn to extend video games or you play Roblox, Lua is a good choice. If you want a general scripting language Lua could be a good choice, but it requires much more work to be useful compared to something like Python or Ruby. I like Ruby, but haven't done much Python. Python is by far the most popular scripting language right now for general desktop scripting.

I use Lua as a general scripting language because I think it's an elegant, fun language to write. Because the Lua language is so small, it means the author (you!) needs to provide a great deal of the logic, where other languages provide larger standard libraries. I like the smaller Lua method because my solutions are only exactly as big as they need to be (tiny), and I have much more control over how it works.

Hope that helps!
Dinsdale
 

Simon

Newcomer
Joined
Nov 19, 2020
Messages
6
Reaction score
0
Age
23
Location
Netherlands
There are lots of similarities, but on a whole the languages are quite different. Lua is a much more simple (I would say elegant) language in many respects.

Javascript is heavily used in webpages and web MVC frameworks like angular, as well as in web services running under nodeJS. Javascript can also be found in NoSQL databases like mongo.

Lua is a small scripting language that is very good for embedding in other applications. This is exemplified by it's use in the game industry. A game can be written to use Lua extensions and users can then create additional content. Roblox, Garry's Mod, World Of Warcraft are just a few names. Lua is also found in bootloaders (FreeBSD), routers (openwrt), and cellular modems (Sierra Wireless).

Both languages can be used as general scripting languages to an extent, however, both have different focuses. NodeJs is heavily web focused. It can be extended through the npm package manager. Lua is VERY small but it can be extended through the LuaRocks Package manager.

Javascript is a better language to learn if you want to do web stuff. Period. If you're young and you want to learn to extend video games or you play Roblox, Lua is a good choice. If you want a general scripting language Lua could be a good choice, but it requires much more work to be useful compared to something like Python or Ruby. I like Ruby, but haven't done much Python. Python is by far the most popular scripting language right now for general desktop scripting.

I use Lua as a general scripting language because I think it's an elegant, fun language to write. Because the Lua language is so small, it means the author (you!) needs to provide a great deal of the logic, where other languages provide larger standard libraries. I like the smaller Lua method because my solutions are only exactly as big as they need to be (tiny), and I have much more control over how it works.

Hope that helps!
Dinsdale
Thx! 😃 Yes, I choose Lua. Because I hawe used Lua for love2d game development before, but I want to embed a scripting language (Lua vs. JavaScript vs. Python) to my own custom C++ engine for the first time.
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
69
Reaction score
27
Location
Victoria BC
Website
winlua.net
Not that I have done a tone of Lua/C++, but I recommend using the Sol 2 project with C++. It's a header only interface between Lua and C++. It removes the necessity to manipulate the Lua stack. Very slick.


The name is a little confusing, I always knew the project as Sol2. He's recently started calling it Sol3, or Sol2 v3. I dunno? It's fast and very slick. Docs are here: sol 3.2 — sol 3.2.3 documentation

I almost forgot about this little nugget: If you want to embed javascript, there is a project by Peter MacDonald called jsish:


JSI is a "javascript-ish" interpreter that can be embedded into a C/C++ application. He used an older version of it very effectively in a project where we worked together a few years back. I was using it as a desktop scripting engine for a short period. It's a very cool project but Peter is an "army of one". Interestingly, Peter MacDonald developed one of the first Linux distributions. He claims it's *the* first, though others disagree. Peter MacDonald (computer programmer) - Wikipedia.
 

Simon

Newcomer
Joined
Nov 19, 2020
Messages
6
Reaction score
0
Age
23
Location
Netherlands
Not that I have done a tone of Lua/C++, but I recommend using the Sol 2 project with C++. It's a header only interface between Lua and C++. It removes the necessity to manipulate the Lua stack. Very slick.


The name is a little confusing, I always knew the project as Sol2. He's recently started calling it Sol3, or Sol2 v3. I dunno? It's fast and very slick. Docs are here: sol 3.2 — sol 3.2.3 documentation

I almost forgot about this little nugget: If you want to embed javascript, there is a project by Peter MacDonald called jsish:


JSI is a "javascript-ish" interpreter that can be embedded into a C/C++ application. He used an older version of it very effectively in a project where we worked together a few years back. I was using it as a desktop scripting engine for a short period. It's a very cool project but Peter is an "army of one". Interestingly, Peter MacDonald developed one of the first Linux distributions. He claims it's *the* first, though others disagree. Peter MacDonald (computer programmer) - Wikipedia.
Thx for the links 😀
I want to learn to embed manually into my C++ engine the first time, but the book recommended by @stetre is a little hard.
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
69
Reaction score
27
Location
Victoria BC
Website
winlua.net

mcc

Newcomer
Joined
Dec 15, 2020
Messages
1
Reaction score
1
To me JavaScript and Lua are near identical languages. Lua is better designed but the "better designed" mostly only comes up in weird edge situations.

To me the big advantage of JavaScript is you have access to TypeScript. TypeScript adds gradual typing to JavaScript (this means that you aren't required to type everything, but if you choose to type something, the TypeScript checker will alert you when the types you claimed get violated). I find my Lua programs often run into big problems when they get large with me getting sloppy about ad hoc structures, I'll have a variable being passed into a function and I won't be able to remember what its fields are. With TypeScript I can just click a variable in Sublime Text and it tells me its type and possible fields.

The big advantage of Lua is that you have access to LuaJIT. It is very hard to embed JavaScript into another binary. You basically have to embed most of a web browser into your application, and I'm not sure I know how to do it efficiently on locked-down platforms like iOS or a game console. There are stripped down JS embed libraries like duktape but I have yet to find one which is *either* efficient or supports ES6.
 

dinsdale247

Moderator
Staff member
Community Patron
Creator of WinLua
Joined
Nov 17, 2020
Messages
69
Reaction score
27
Location
Victoria BC
Website
winlua.net
To me JavaScript and Lua are near identical languages. Lua is better designed but the "better designed" mostly only comes up in weird edge situations.

To me the big advantage of JavaScript is you have access to TypeScript. TypeScript adds gradual typing to JavaScript (this means that you aren't required to type everything, but if you choose to type something, the TypeScript checker will alert you when the types you claimed get violated). I find my Lua programs often run into big problems when they get large with me getting sloppy about ad hoc structures, I'll have a variable being passed into a function and I won't be able to remember what its fields are. With TypeScript I can just click a variable in Sublime Text and it tells me its type and possible fields.
There have been multiple attempts to create a typed Lua. I have minimally tried Ravi - Ravi Programming Language by ravilang

I think Ravi is the epitome of the difference between Lua and Javascript. Javascript is one thing and it has a very strict standard. People expect that standard. Lua is a loosely defined API that can be extended by anyone because it's so...small.
The big advantage of Lua is that you have access to LuaJIT. It is very hard to embed JavaScript into another binary. You basically have to embed most of a web browser into your application, and I'm not sure I know how to do it efficiently on locked-down platforms like iOS or a game console. There are stripped down JS embed libraries like duktape but I have yet to find one which is *either* efficient or supports ES6.


JSI is an embedable javascript interpreter created by Peter MacDonald who holds claim to creating one of the first (THE first?) Linux distributions (SLS Linux [1]). I have used JSI in an application we worked on together. I have also used JSI a number of times as a scripting engine. The C API is much better than Lua in my opinion, but I don't really know either API very well.

[1] As per the claim of buggy-ness, Peter pointed out that he was going to university, working as a full time systems administrator for the BC government, and raising a family with three kids.
 

darkwiiplayer

Newcomer
Joined
May 30, 2020
Messages
10
Reaction score
2
Age
26
Location
Germany
but the "better designed" mostly only comes up in weird edge situations.
Not at all. JS is filled with so many bad design decisions that it shows all the time. Weird cases like the empty string being treated as falsey in if statements break many techniques that you can easily use in Lua, to name one example.

To me the big advantage of JavaScript is you have access to TypeScript
Lua has tl now, which is already looking very promising. That basically does the same thing as TypeScript but for Lua.

Lua programs often run into big problems when they get large with me getting sloppy about ad hoc structures
Don't use so many ad hoc structures then. You can easily build a simple class system or use one as a library. If you properly document your classes, the only difference is that you might have to navigate the documentation manually or teach your editor where to look it up.
 

ET.

Newcomer
Joined
Dec 21, 2020
Messages
1
Reaction score
0
I almost forgot about this little nugget: If you want to embed javascript, there is a project by Peter MacDonald called jsish:


JSI is a "javascript-ish" interpreter that can be embedded into a C/C++ application. He used an older version of it very effectively in a project where we worked together a few years back. I was using it as a desktop scripting engine for a short period. It's a very cool project but Peter is an "army of one". Interestingly, Peter MacDonald developed one of the first Linux distributions. He claims it's *the* first, though others disagree. Peter MacDonald (computer programmer) - Wikipedia.
There's also Civetweb which is effectively JSI but with lua instead, and with sqlite, lfs, xml support builtin, also embeddable. It also seems to be a one man project.
The big advantage of Lua is that you have access to LuaJIT. It is very hard to embed JavaScript into another binary. You basically have to embed most of a web browser into your application, and I'm not sure I know how to do it efficiently on locked-down platforms like iOS or a game console. There are stripped down JS embed libraries like duktape but I have yet to find one which is *either* efficient or supports ES6.
Have you heard of quickjs? Its another small and embeddable Javascript engine that supports ES2020 which supersedes ES6(ES2015), but by no means the most efficient. Created by the same person who wrote ffmpeg, qemu and tcc. That has spurred a whole host projects downstream like txiki.js a tiny event loop JavaScript runtime in the likes of Deno, and Sciter.JS a mini web engine compared to electron.
 
Top