Home >Web Front-end >Front-end Q&A >What is the difference between lua and javascript
Difference: 1. The comment code of JavaScript is "//", and the comment code of Lua is "--"; 2. JavaScript uses val to declare global variables and there are no local variables, while Lua does not need to be directly positioned. are global variables, and local declarations are local variables.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
What is the difference between lua and javascript
Comparison of the differences between Lua and Javascript
Lua simulator js solution
1. Syntax level simulation
Differences between lua and js language
1.1 Comments
js is //, lua is --.
1.2 Variables
js uses val to declare global variables and there are no local variables, while lua does not need to be positioned directly. are global variables, and local declarations are local variables.
1.3 Operator
js
- * / % --
= = -= *= /= %=
Supported strings
txt1 = "what a very";
txt2 = "nice day";
txt3 =txt1 " " txt2;
The output result of printing txt3 is "what a very nice day".
Rule:
Compare numbers with strings Add, the result will become a string.
lua
binary: - * / ^ %
uniary:- (Minus sign)
lua strings are spliced into ..
For example, "Hello ".."World" is spliced into Hello World
1.4 Relational Operator
js relational operator
== === (congruent) != > 6d267e5fab17ea8bc578f9e7e5e1570b= < =
lua relational operator
1.5 Logical operators
if then
elseif then
else
end
There must be end
1.7 Switch statementlua does not support Switch statement
1.8 Message box
Confirm box prompt("text","default value")
##js
function 函数名(参数) { 代码... }js with {} class c
function 函数名( 参数) endlua class vb script
2.0 For loop
js: Class c
for (i=0;i<=10;i++) { document.write("The number is " + i) document.write("<br />") }lua: divided into two types: numeric type For and generic Fornumeric type For:
for var= exp1,exp2,exp3 do <执行体> endvar changes from exp1 to exp2, step is incremented by exp3If exp3 is not specified, the default is 1
for i =1,100 do print(i) end for i =1,100,2 do print(i) endGeneric ForThe generic For loop traverses all values through an iterator (iterator) function :
--打印数组a 的所有值 for i,v in pairs(a) do print(v) endThe Lua basic library provides ipairs, which is an iterator function for traversing arrays. In each loop i will be assigned an index value, and v will be assigned an array element value corresponding to the index.
---打印table t中所以的key for k in pairs(t) do print(k) end
2.1 While loop
js: Class c
while (变量<=结束值) { 需执行的代码 } lua: i =1; while a[i] do print(a[i]) i = i+1; endAt the same time, lua also supports repeat: supports repeat-until statement to implement loop.
repeat: line = io.read() until line~="" print(line)The above code: Read line until the end when line is not "", and print the value of this line.
2.2 Break and Continue
js: There are two types of c
<script type="text/javascript"> var i=0 for (i=0;i<=10;i++) { if (i==3){break} document.write("The number is " + i) document.write("<br />") } </script> Continuecontinue command will terminate the current loop and continue running from the next value.
<script type="text/javascript"> var i=0 for (i=0;i<=10;i++) { if (i==3){continue} document.write("The number is " + i) document.write("<br />") } </script>Lua:
local i =1 while a[i] do if a[i] == v then break end i = i +1 end
2.3 For... In statement
js: Use the For...In statement to specifically traverse the elements in the array.
For...In statement is used to loop through the properties of an array or object. for ... in Each time the code in the loop is executed, an operation will be performed on the elements of the array or the properties of the object. Syntax:for (变量 in 对象) { 在此执行代码 }Code:
<script type="text/javascript"> var x var mycars = new Array() mycars[0] = "Saab" mycars[1] = "Volvo" mycars[2] = "BMW" for (x in mycars) { document.write(mycars[x] + "<br />") } </script>Lua: It is very simple to replace it with the generic For.Related recommendations:
javascript learning tutorial
The above is the detailed content of What is the difference between lua and javascript. For more information, please follow other related articles on the PHP Chinese website!