javascript语法_JavaScript语法
javascript语法
句法 (Syntax)
Any programming/scripting language has some syntax associated with it. The syntax is a set of rules for writing that language. The grammar equivalent of English. Any deviation from these rules results either in a logical or runtime error. Although JavaScript is quite flexible in terms of what you want to do with it, we still need to follow certain rules while coding down our logic in it.
任何编程/脚本语⾔都有⼀些与之关联的语法。 语法是⽤于编写该语⾔的⼀组规则。 相当于英语的语法。 违反这些规则会导致逻辑错误或运⾏时错误。 尽管JavaScript在您要使⽤的功能⽅⾯⾮常灵活,但是在编写逻辑代码时,我们仍然需要遵循某些规则。
浏览器内置的开发⼈员控制台 (Browser's in built developer console)
You can start using JavaScript directly on a browser. Open a browser of your choice
select
right-click anywhere and select
Open a browser of your choice, right-click anywhere
Go to the console. Here anything you write will be validated as inspect
inspect. You'll get a small window docked to the bottom. Go to the console
JavaScript.
选择检查 。 您会看到⼀个固定在底部的
右键单击任意位置,然后选择检查
您可以直接在浏览器上开始使⽤JavaScript。 打开您选择的浏览器
打开您选择的浏览器 , 右键单击任意位置,
转到控制台 。 在这⾥,您编写的任何内容都将被验证为JavaScript。
⼩窗⼝。 转到控制台
some basic rules to write any JavaScript code.
Let's look at some basic rules to write any JavaScript code
编写任何JavaScript代码的⼀些基本规则 。
让我们看⼀下编写任何JavaScript代码的⼀些基本规则
Operators and Operations
运营商和运营
+, -, /, *, % it means you are trying to perform arithmetic operations of addition, subtraction, division,
If you use +, -, /, *, %
multiplication, and remainder respectively. Try these out.
+,-,/,*,%,则表⽰您试图分别执⾏加,减,除,乘,除的算术运算。 试试看。
如果使⽤+,-,/,*,%,
网站底部代码js特效
These operators work differently with values having different data types. For example, + can be used to concatenate two strings.
这些运算符对具有不同数据类型的值的处理⽅式不同。 例如,+可⽤于连接两个字符串。
Variables
变数
To remember a value, you need variables. Variables store the values in memory and you can reference them by their name. We use the var keyword to declare a variable.
要记住⼀个值,您需要变量。 变量将值存储在内存中,您可以按其名称引⽤它们。 我们使⽤var关键字声明⼀个变量。
var variable_name = value
Being a weakly typed language, you need not specify the type of value your variable is storing. Simply declare it with a name and using the assignment operator, assign the value on the right to the variable on the left of it.
作为弱类型语⾔,您⽆需指定变量存储的值的类型。 只需使⽤名称进⾏声明,然后使⽤赋值运算符,即可将其右侧的值分配给其左侧的变量。
Note: JavaScript like other languages is case sensitive. Two variables with the same name are different if they have the Note:
same name but in different cases.
注意: JavaScript和其他语⾔⼀样,区分⼤⼩写。 如果两个名称相同但在不同情况下名称相同,则两个变量将不同。
注意:
Also, a variable name must be a continuous segment of characters. It should not contain any spaces.
另外,变量名称必须是字符的连续段。 它不能包含任何空格。
You can use the double equal to (==) operator
double equal to (==) operator to check if the value on the right is the same as the value on the left. Remember that it is not the same assignment operator that we have used above.
双精度等于(==)运算符来检查右侧的值是否与左侧的值相同。 请记住,它与我们上⾯使⽤的赋值运算符不同。
您可以使⽤双精度等于(==)运算符
在HTML中使⽤JavaScript (Using JavaScript in HTML)
In real-life applications, you will not be writing JavaScript on the dev console but in a text editor. So let's see how we attach JavaScript to an HTML page.
在实际的应⽤程序中,您不会在开发控制台上编写JavaScript,⽽是在⽂本编辑器中编写JavaScript。 因此,让我们看看如何将JavaScript附加到HTML页⾯。
Open any text editor of your choice. One of the most popular ones is visual studio code, you can download it from here:
打开您选择的任何⽂本编辑器。 最受欢迎的⼀种是Visual Studio代码,您可以从此处下载: :
index.html.
Create an empty folder and add a file named index.html
index.html的⽂件。
创建⼀个空⽂件夹并添加⼀个名为index.html
You can add Javascript to your HTML file either <script> tags at the end. Anything between <script> </script> will be
.js. In that case, we specify an attribute validated as JavaScript. You can also write it in a separate file with an extension of .js
of <script> tag src (source) to the relative path of your file. The former method is useful when you don't have a large amount of code whereas later is used otherwise.
您可以在末尾的<script>标记中将Javascript添加到HTML⽂件中。 <script> </ script>之间的任何内容都将被验证为JavaScript。 您也.js的单独⽂件中。 在这种情况下,我们为⽂件的相对路径指定<script>标记src (源)的属性。 当您没有⼤量代码
可以将其写⼊扩展名为.js
时,前⼀种⽅法很有⽤,否则则使⽤后⼀种⽅法。
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Simple JavaScript syntax</h1>
<script>
//you can add comments using this
var s='Hello world!'
console.log(s);
document.write(s);
alert('JavaScript is fun!');
</script>
</body>
</html>
/
/ are considered as comments, which are meant for the developer and not the user. It is for your reference Anything after //
and will have no meaning for the code.
//后⾯的所有内容均视为注释,仅供开发⼈员⽽⾮⽤户使⽤。 仅供参考,对代码没有任何意义。
console.log() method and something directly to the browser using the
You can write anything to the console using console.log()
document.write() method.
document.write()
console.log()⽅法将任何内容写⼊控制台,并使⽤document.write()
document.write()⽅法将任何内容直接写⼊浏览器。
您可以使⽤console.log()
alert() function displays a pop up on the web page with the text specified inside it.
The alert()
alert()函数在⽹页上显⽰⼀个弹出窗⼝,其中包含指定的⽂本。
alert()
The syntax for using the alter keyword :
使⽤alter关键字的语法:
alert('string_to_be_printed')
opening it up with a browser. Run this file by right-clicking index.html
index.html and opening it up with a browser
使⽤浏览器将其打开,以运⾏该⽂件。
右键单击index.html
index.html并使⽤浏览器将其打开,以
翻译⾃:
javascript语法

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。