Home Web Front-end JS Tutorial Organize Javascript basic syntax study notes_javascript skills

Organize Javascript basic syntax study notes_javascript skills

May 16, 2016 pm 03:29 PM
javascript grammar

1. What is a variable
Literal meaning: A variable is a variable amount;
Programming perspective: A variable is a memory used to store some/certain values. We can think of a variable as a box, which is used to store items. The items can be clothes, toys, fruits, etc.

2. Express your thoughts (expression)
An expression is similar to the definition in mathematics. An expression refers to an algebraic expression that has a certain value and uses operators to connect constants and variables. An expression can contain constants or variables.

String expression: "I" "love" "you" mychar //Write a string expression, the value is a string.

Numerical expression: num 5*32 2.5 //Write a numeric expression, the value is a numeric value.

Boolean expression: 2>3 num==5 num<60 //Write an expression of Boolean value true or false

Xiao Ming had 10 yuan and bought a notebook for 5 yuan. Xiaohong said: "With your remaining money and my 6 yuan, you can buy a pencil case."

 <script type="text/javascript">
  var num1 = 10-5;//计算小明剩下多少钱
  var num2 = num1+6;//小红花多少钱买铅笔盒
 document.write("小明还剩:"+num1+"元"+"<br>");
 document.write("小红花:"+num2+"元买个铅笔盒");
 </script>
Copy after login

3. Sign operator
Operators are symbols used to specify certain actions in JavaScript.
(1) Operator
  sun = numa numb;
Among them, "=" and " " are both operators.
Arithmetic operators ( , -, *, /, etc.)
Comparison operators (<, >, >=, <=, etc.)
Logical operators (&&, ||, !).
Note: The "=" operator is assignment, not equal.
(2) " " operator
In JavaScript, " " not only represents addition, but can also concatenate two strings.

Copy code The code is as follows:
mystring = "Java" "Script";//the value of mystring" JavaScript" this string

4. Add one to yourself and decrement one to yourself (and - -)
In addition to the arithmetic operators (, -, *, /), there are two very commonly used operators, increment by " "; decrement by "--". First let’s look at an example:

mynum = 10;
mynum++; //mynum的值变为11
mynum--; //mynum的值又变回10
Copy after login

In the above example, mynum increases the value of mynum by 1 based on the original value, and mynum-- causes mynum to decrease by 1 based on the original value. In fact, it can also be written as:

 mynum = mynum + 1;//等同于mynum++
 mynum = mynum - 1;//等同于mynum-- 
Copy after login

5. Comparison operators
Let’s do a math question first. Among the math test scores, Xiao Ming scored 90 points and Xiao Hong scored 95 points. Who got the higher score?
Answer: Because "95 > 90", Xiaohong's test score is high.
The greater than sign ">" is the comparison operator, and Xiaohong's test scores and Xiaoming's test scores are the operands, and they are two operands.
That is to say, the two operands are compared through the comparison operator, and the values ​​obtained by are true (true) and false (false).
Operator meaning:
< less than
> Greater than
<= less than or equal to
>= greater than or equal to
== equals
!= is not equal to

 var a = 5;//定义a变量,赋值为5
 var b = 9; //定义b变量,赋值为9
 document.write (a<b); //a小于b的值吗&#63; 结果是真(true)
 document.write (a>=b); //a大于或等于b的值吗&#63; 结果是假(false)
 document.write (a!=b); //a不等于b的值吗&#63; 结果是真(true)
 document.write (a==b); //a等于b的值吗&#63; 结果是假(false)
Copy after login

The equality operator == does not mean strict equality. For example: What will be the result of comparing false with an empty string?

 var a = false;
 var b = "";
 if(a == b){
  alert("a equals b");
 }
 //这个条件语句的求值结果是true。因为相等操作符==认为空字符串于false的含义相同。要进行严格比较,就要使用另一种等号(===)。这个全等操作符会执行严格的比较,不仅比较值,而且会比较变量的类型:
 var a = false;
 var b = "";
 if (a === b){
  alert("a equals b");
 }
Copy after login

This time, the evaluation result of the conditional expression is false. Because even though false has the same meaning as the empty string, Boolean and String are not the same type.

The same is true for the inequality operator !=. If you want strict inequality, use !==.

6. Logical AND operator
In mathematics, "a>b" also means "a>b" in JavaScript; in mathematics, "b is greater than a, b is less than c" is "a b>a && b For example, when we take the college entrance examination, we must present our admission ticket and ID card before entering the examination room. Both are indispensable, otherwise we will not be able to take the exam. The expression is as follows:

 if(有准考证 &&有身份证) 
 {
 进行考场考试
 }
Copy after login

“&&”是逻辑与操作符,只有“&&”两边值同时满足(同时为真),整个表达式值才为真。
逻辑与操作符值表:

注意: 如果A为假,A && B为假,不会在执行B; 反之,如果A为真,要由 B 的值来决定 A && B 的值。

7、我或你都可以 (逻辑或操作符)
"||"逻辑或操作符,相当于生活中的“或者”,当两个条件中有任一个条件满足,“逻辑或”的运算结果就为“真”
逻辑或操作符值表:

注意: 如果A为真,A || B为真,不会在执行B; 反之,如果A为假,要由 B 的值来决定 A || B 的值

<script type="text/javascript">
 var numa,numb,jq1;
 numa=50;
 numb=55;
 jq1= numa>numb||numa==numb;
 document.write("jq1的值是:"+jq1+"<br>")
</script>

Copy after login

8、是非颠倒(逻辑非操作符)
"!"是逻辑非操作符,也就是"不是"的意思,非真即假,非假即真。
逻辑非操作符值表:

例:

 var a=3;
var b=5;
var c;
c=!(b>a); // b>a值是true,! (b>a)值是false
c=!(b<a); // b<a值是false, ! (b<a)值是true
Copy after login
<script type="text/javascript">
  var numa,numb,jq1;
  numa=60;
  numb=70;
  jq1=!(numa<numb);
  document.write("jq1的值是:"+jq1+"<br>")//输出值jq1的值是:false
</script> 
Copy after login

保持先后顺序(操作符优先级)
例一:

var numa=3;

 var numb=6
jq= numa + 30 / 2 - numb * 3; // 结果为0
Copy after login

例二:

var numa=3;
var numb=6
jq= ((numa + 30) / (2 - numb)) * 3; //结果是-24.75
Copy after login

操作符之间的优先级(高到低):
算术操作符 → 比较操作符 → 逻辑操作符 → "="赋值符号
如果同级的运算是按从左到右次序进行,多层括号由里向外。

 var numa=3;
 var numb=6;
 jq= numa + 30 >10 && numb * 3<2; //结果为false

 <script type="text/javascript">
  var numa,numb,jq1;
  numa=5;
  numb=2;
  jq1=numa + 30 >10 && numb * 3<20;
  jq2=((numa + 30) / (7-numb)) * 3

  document.write("jq1的值是:"+jq1+"<br>");//jq1的值是:true
 document.write("jq2的值是:"+jq2);//jq2的值是:21
</script>

<script type="text/javascript">
  var a,b,sum;
  var a = 5;
  var b = 100%7; 
  sum = a > b && a*b > 0 ;
  document.write( "我认为 a 的值是:" + 5 + " b的值是:" + 2 + "sum 的值是:" + true+"<br/>");
  document.write( "答案是,第一轮计算后,a 为:"+ a +";b为:"+b +";第一次计算sum为:"+ sum +"<br/>");
 
  sum = ( (++a) + 3 ) / (2 - (--b) ) * 3; 
  document.write( "再一次计算后,我认为 a 的值是:" + 6 + " b的值是:" + 1 + "sum 的值是:" + 27 +"<br/>"); 
  document.write( "答案是,第二轮计算后,a 为:" + a + ";b为:" + b +";第二次计算sum为:"+ sum +",sum的类型也发生变化了。");
</script>

Copy after login

 以上就是关于Javascript基础语法的全部内容,希望对大家的学习有所帮助。

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks 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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

What are the syntax and structure characteristics of lambda expressions? What are the syntax and structure characteristics of lambda expressions? Apr 25, 2024 pm 01:12 PM

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

The connection and difference between Go language and JS The connection and difference between Go language and JS Mar 29, 2024 am 11:15 AM

The connection and difference between Go language and JS Go language (also known as Golang) and JavaScript (JS) are currently popular programming languages. They are related in some aspects and have obvious differences in other aspects. This article will explore the connections and differences between the Go language and JavaScript, and provide specific code examples to help readers better understand these two programming languages. Connection: Both Go language and JavaScript are cross-platform and can run on different operating systems.

See all articles