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) != > < >= < =
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 函数名(参数) { 代码... }
function 函数名( 参数) end
2.0 For loop
js: Class c
for (i=0;i<=10;i++) { document.write("The number is " + i) document.write("<br />") }
for var= exp1,exp2,exp3 do <执行体> end
for i =1,100 do print(i) end for i =1,100,2 do print(i) end
--打印数组a 的所有值 for i,v in pairs(a) do print(v) end
---打印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; end
repeat: line = io.read() until line~="" print(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> Continue
<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>
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 对象) { 在此执行代码 }
<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>
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!