Home >Web Front-end >Front-end Q&A >What is the difference between lua and javascript

What is the difference between lua and javascript

WBOY
WBOYOriginal
2022-01-19 16:04:263921browse

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.

What is the difference between lua and javascript

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

##6d267e5fab17ea8bc578f9e7e5e1570b b207bb093495751f3e536c5eb7122819= == ~=(not equal to)

1.5 Logical operators

    ##js
  • && || !

    lua
  • and or not

1.6 If ...Else statement

    js class c
  • if else

    lua
  • if then else

if then

elseif then

else

end

There must be end

1.7 Switch statement

lua does not support Switch statement

1.8 Message box

    js
  • Alert box alert("text")

Confirm box prompt("text","default value")

    lua
  • Extended support for warning boxes and confirmation boxes

1.9 Function

##js
  • function 函数名(参数)
    {
      代码...
    }

    js with {} class c

lua
  • function 函数名( 参数)
    end

    lua 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 For

numeric type For:

for var= exp1,exp2,exp3 do
<执行体>
end

var changes from exp1 to exp2, step is incremented by exp3

If exp3 is not specified, the default is 1

for i =1,100 do 
print(i)
end
for i =1,100,2 do 
print(i)
end

Generic For

The generic For loop traverses all values ​​through an iterator (iterator) function :

--打印数组a 的所有值
for i,v in pairs(a) do print(v) end

The 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;
end

At 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
  • that can be used in loops Statements in: break and continue
Break

The break command can terminate the running of the loop and then continue to execute the code after the loop (if there is code after the loop).

Code example:

<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

continue 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:
  • supports break, but does not support continue.
  • 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn