In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the basic features of JavaScript". In daily operation, I believe many people have doubts about the basic features of JavaScript. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the basic features of JavaScript?" Next, please follow the editor to study!
Code structure
Statements are separated by semicolons:
Alert ('Hello'); alert (' World')
In general, newline characters are also considered delimiters, so the following example works as well:
Alert ('Hello') alert (' World')
This is called "automatic semicolon insertion". But sometimes it doesn't work, for example:
Alert ("There will be an error after this message") [1,2] .forEach (alert)
Most code style guidelines agree that we should put a semicolon after each statement.
There is no need to add a semicolon after the code block {.} and syntax structures with code blocks (such as loops):
Function f () {/ / function declaration does not need to add semicolon} for (;;) {/ / loop statement does not need to add semicolon}
…… But even if we add an "extra" semicolon somewhere, it's not a mistake. The semicolon will be ignored.
More: code structure.
Strict mode
To fully enable all the features of modern JavaScript, we should write the "use strict" directive at the top of the script.
'use strict';...
The instruction must be at the top of the JavaScript script or at the beginning of the function body.
Without "use strict", everything still works, but some features behave in the same way as the old "compatible" way. We usually prefer the modern way.
Some modern features of the language, such as the classes we will learn in the future, implicitly enable strict patterns.
More: modern model, use strict.
Variable
You can declare variables in the following ways:
Let
Const (immutable, unchangeable)
Var (old-fashioned, as you'll see later)
A variable name can consist of the following:
Letters and numbers, but the first character cannot be a number.
The characters $and _ are allowed and are used in the same letter.
Non-Latin letters and hieroglyphs are also allowed, but are not usually used.
Variables are dynamically typed, and they can store any value:
Let x = 5; x = "John"
There are 7 data types:
Number-can be a floating point number or an integer
String-string type
Boolean-logical value: true/false
Null-A type with a single value null, which means "empty" or "does not exist"
Undefined-Type with a single value undefined, meaning "unassigned (undefined)"
Object and symbol-for complex data structures and unique identifiers, we haven't learned this type yet.
The type of value returned by the typeof operator, with two exceptions:
Typeof null = "object" / / Design error of JavaScript programming language typeof function () {} = = "function" / / function is treated specially
More: variables and data types.
Interaction
We use browsers as our work environment, so the basic UI functions will be:
Prompt (question [, default]): asks a question and returns what the visitor typed, and returns null if he presses cancel.
Confirm (question): ask a question and advise the user to choose between "OK" and "cancel". The selection result is returned in the form of true/false.
Alert (message): outputs a message.
These functions generate modal boxes that pause code execution and prevent visitors from interacting with the rest of the page until the user answers.
For example:
Let userName = prompt ("Your name?", "Alice"); let isTeaWanted = confirm ("Do you want some tea?"); alert ("Visitor:" + userName); / / Alice alert ("Tea wanted:" + isTeaWanted); / / true
More: alert, prompt, and confirm interactions.
Operator
JavaScript supports the following operators:
Arithmetic operators: regular: +-* / (addition, subtraction, multiplication and division), take the remainder operator% and the power operator *.
The binary plus sign `+` can connect strings. If any Operand is a string, the other Operand will also be converted to a string: ```js run alert ('1' + 2); / /'12', the string alert (1 +'2'); / /'12', the string```
Assignment
Simple assignment: a = b and combined with other operations: a * = 2.
Bitwise operators operate on 32-bit integers at the lowest level: see the documentation for details.
Ternary operator
The only operation with three parameters: cond? ResultA: resultB . Returns resultA if cond is true, resultB otherwise.
Logical operator
Logic and & & and or | perform a short-circuit operation and then return the value where the operation stops (true/false is not required). Logic is not! Converts the Operand to a Boolean value and returns its opposite value.
Comparison operator
When you check for equality on different types of values, the operator = = converts different types of values to numbers (except for null and undefined, which are equal to each other and nothing else), so the following example is equal:
````js alert (0 = = false); / / true alert (0 = =''); / / true ```other comparisons will also be converted to numbers. The strict equality operator `=` is not converted: different types always refer to different values. The values `null` and `undefined` are special: they are only equal under `= =` and are not equal to any other value. Greater than / less than comparison, when comparing strings, they are compared character by character in character order. Other types are converted to numbers.
Other operators
There are a small number of other operators, such as the comma operator.
More: operators, comparison of values, logical operators.
Cycle
We cover three types of loops:
/ / 1 while (condition) {...} / / 2 do {...} while (condition); / / 3 for (let I = 0; I
< 10; i++) { ... } 在 for(let...) 循环内部声明的变量,只在该循环内可见。但我们也可以省略 let 并重用已有的变量。 指令 break/continue 允许退出整个循环/当前迭代。使用标签来打破嵌套循环。 更多内容:while 和 for 循环。 稍后我们将学习更多类型的循环来处理对象。 "switch" 结构 "switch" 结构可以替代多个 if 检查。它内部使用 ===(严格相等)进行比较。 例如: let age = prompt('Your age?', 18); switch (age) { case 18: alert("Won't work"); // prompt 的结果是一个字符串,而不是数字 case "18": alert("This works!"); break; default: alert("Any value not equal to one above"); } 详情请见:"switch" 语句。 函数 我们介绍了三种在 JavaScript 中创建函数的方式: 1.函数声明:主代码流中的函数 function sum(a, b) { let result = a + b; return result; } 2.函数表达式:表达式上下文中的函数 let sum = function(a, b) { let result = a + b; return result; } 3.箭头函数: // 表达式在右侧 let sum = (a, b) =>A + b; / or multiline syntax with {...}, where you need return: let sum = (a, b) = > {/ /. Return a + b;} / / has no parameter let sayHi = () = > alert ("Hello"); / / has one parameter let double = n = > n * 2
A function may have a local variable: a variable declared within the function. Such variables are visible only within the function.
Parameters can have default values: function sum (a = 1, b = 2) {.}.
Functions always return something. If there is no return statement, the result returned is undefined.
At this point, the study on "what are the basic features of JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.