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

What is the difference between lua and javascript

Jan 19, 2022 pm 04:04 PM
html javascript front end

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) != > < >= < =

  • lua relational operator

##< > <= >= == ~=(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 函数名(参数)
    {
      代码...
    }
    Copy after login

    js with {} class c

lua
  • function 函数名( 参数)
    end
    Copy after login

    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 />")
}
Copy after login

lua: divided into two types: numeric type For and generic For

numeric type For:

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

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
Copy after login

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
Copy after login

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
Copy after login

2.1 While loop

js: Class c

while (变量<=结束值)
{
    需执行的代码
}
lua:
i =1;
while a[i] do
print(a[i])
i = i+1;
end
Copy after login

At the same time, lua also supports repeat: supports repeat-until statement to implement loop.

repeat:
 line = io.read()
until line~=""
print(line)
Copy after login

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
Copy after login

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>
Copy after login

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
    Copy after login

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 对象)
{
    在此执行代码
}
Copy after login

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>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles