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, right-click anywhere and select inspect. You'll get a small window docked to the bottom. Go to the console. Here anything you write will be validated as JavaScript.

您可以直接在浏览器上开始使用JavaScript。 打开您选择的浏览器右键单击任意位置,然后选择检查 。 您会看到一个固定在底部的小窗口。 转到控制台 。 在这里,您编写的任何内容都将被验证为JavaScript。

javascript syntax 1

Let's look at some basic rules to write any JavaScript code.

让我们看一下编写任何JavaScript代码的一些基本规则

Operators and Operations

运营商和运营

If you use +, -, /, *, % it means you are trying to perform arithmetic operations of addition, subtraction, division, multiplication, and remainder respectively. Try these out.

如果使用+,-,/,*,%,则表示您试图分别执行加,减,除,乘,除的算术运算。 试试看。

javascript syntax 2

These operators work differently with values having different data types. For example, + can be used to concatenate two strings.

这些运算符对具有不同数据类型的值的处理方式不同。 例如,+可用于连接两个字符串。

javascript syntax 3

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

javascript syntax 4

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 same name but in different cases.

注意: JavaScript和其他语言一样,区分大小写。 如果两个名称相同但在不同情况下名称相同,则两个变量将不同。

Also, a variable name must be a continuous segment of characters. It should not contain any spaces.

另外,变量名称必须是字符的连续段。 它不能包含任何空格。

javascript syntax 5

You can use the 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: https://code.visualstudio.com/

打开您选择的任何文本编辑器。 最受欢迎的一种是Visual Studio代码,您可以从此处下载: https : //code.visualstudio.com/

Create an empty folder and add a file named index.html.

创建一个空文件夹并添加一个名为index.html的文件。

You can add Javascript to your HTML file either <script> tags at the end. Anything between <script> </script> will be validated as JavaScript. You can also write it in a separate file with an extension of .js. In that case, we specify an attribute 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 (源)的属性。 当您没有大量代码时,前一种方法很有用,否则则使用后一种方法。

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

Anything after // are considered as comments, which are meant for the developer and not the user. It is for your reference and will have no meaning for the code.

//后面的所有内容均视为注释,仅供开发人员而非用户使用。 仅供参考,对代码没有任何意义。

You can write anything to the console using console.log() method and something directly to the browser using the document.write() method.

您可以使用console.log()方法将任何内容写入控制台,并使用document.write()方法将任何内容直接写入浏览器。

The alert() function displays a pop up on the web page with the text specified inside it.

alert()函数在网页上显示一个弹出窗口,其中包含指定的文本。

The syntax for using the alter keyword :

使用alter关键字的语法:

    alert('string_to_be_printed') 

Run this file by right-clicking index.html and opening it up with a browser.

右键单击index.html使用浏览器将其打开,以运行该文件。

javascript syntax 5
javascript syntax 5

翻译自: https://www.includehelp.com/code-snippets/javascript-syntax.aspx

javascript语法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值