Introduction to JavaScript Syntax 1.JS 1-introduced directly under <script> tags
<!DOCTYPE HTML><HTMLLang= "ZH-CN"><Head> <MetaCharSet= "UTF-8"> <title>Title</title> <Script>Console.log ('Hello world!'); </Script></Head><Body></Body></HTML>
Right-click in the browser to select Check, select Console, you can see the output of the code
2-Import JS code from the file
HTML code
<!DOCTYPE HTML><HTMLLang= "ZH-CN"><Head> <MetaCharSet= "UTF-8"> <title>Title</title> <Scriptsrc= "Js/helloworld.js"></Script></Head><Body></Body></HTML>
JS Code
Console.log (' Hello world! ');
Results Ibid.
2. Grammar rules
The study procedure, is has the law to follow, is the procedure has the same part, these parts are one kind of stipulation, cannot change, we become: the grammar.
(1) JavaScript is not sensitive to line, indent, and space.
Note: Each statement ends with a semicolon, although the semicolon is not mandatory, but for the program to be compressed in the future, if not semicolon, after compression will not run.
(2) All the symbols are in English. such as parentheses , quotation marks, semicolons.
(3) JavaScript comments:
<script type= "Text/javascript" > // single line comment / * Multiline Comment 1 Multiline Comment 2 */</script>
Note: In Pycharm, Sublime, the shortcut key of single-line comment is ctrl+/, the shortcut key of multiline comment is ctrl+shift+/.
3.JavaScript How to output information in a Web page 3.1-Popup Alarm box: Alert (")
// pop-up Alarm box alert (' This is the Alarm box ');
After opening the browser, an alarm box will pop up. The purpose of alert (translated as "alert"): pops up "warning box".
3.2-Console output: Console.log (")
// Console console console.log (' This is the console ');
console.log("")
Represents the output in the console. Console indicates "console", and log indicates "output".
The console is in the Chrome browser's F12. The console is where engineers and programmers debug programs. Programmers often use this statement to output something to test whether the program is correct.
3.3-User input: prompt () statement
// user Input var a = prompt ("User input information"); Console.log (a);
prompt()
is specifically used to pop up a dialog box that allows the user to enter.
4. Variable 4.1-Define Variable
// Defining Variables var a = +; var =Console.log (A, b);
var is the abbreviation for English "variant" variables. Add a space to the back, and the thing behind the space is the variable name.
Define variables: var is a keyword used to define variables. The so-called keyword, is a special function of the small words. The keyword must be separated by a space.
Assignment of a variable: the equals sign assigns the value to the right of the equal sign to the left variable.
Variable name: we can give the variable any name.
PS: in JavaScript, always use var to define variables
4.2-Naming conventions for variables
Variable names have a naming convention: they can only consist of English letters, numbers, underscores, dollar sign $, and cannot start with a number, and cannot be a JavaScript reserved word.
Common keywords-not available
Abstract,boolean,byte,char, class, const,debugger,double, enum, Export, extends, final,float, gotoimplements, import,int, interface,long, native , package, private, protected, public,short, Static, super, synchronized, throws, transient, volatile
Sensitive to case
4.3-Types of variables
Variables can store numbers, strings, and so on. Variables automatically determine their type based on the type of stored content.
You can use the TypeOf method to get the type of a variable
Number Type
in JavaScript, as long as it is a number, it is the numeric type . Regardless of the float, floating point number (that is, decimal), regardless of size, whether positive or negative, is number type.
// Number Type var a = 100; var b = 100.1; var c =-100; typeoftypeoftypeof C);
String type
Content enclosed in quotation marks
// String Type var a = ' word '; var b = ' word '; var c = ' 12313 '; var d = '; typeof typeof typeoftypeof D);
You can use hyphens to stitch strings
// String Type var a = ' word '; var b = ' word '; var c = ' 12313 '; var d = '; typeof typeof typeoftypeof D ); Console.log (a+b+c+d);
4.4-Operation of variables
// string-numeric = numeric var a = ' str '; var b = $; var c = ' + '; Console.log (a typeof A- b); Console.log (ctypeof b); // Numeric + string typeof (B + a)); Console.log (typeof (B+c));
string-value = NaN
Value-string = NaN
string + numeric = string
numeric + string = string
4.5-Variable format conversion
1-prompt input using the user input method is a string
// user input type var inp = prompt (' user input type 'typeof InP);
2-use parseint (): Convert strings to numbers
parseint () to convert a string to a number . Parse represents "transform", and int denotes "integer" (note Int
the spelling).
Characteristics:
(1) With the function of automatic purification, only the number of the beginning of the string is preserved , the following Chinese automatically disappears.
// parseint () string converted to digital var str1 = "2018"; Console.log (parseint (str1));
(2) Automatically with truncation decimal function: Rounding , not rounding .
var a = 5.5; var b = 2.3; Console.log (parseint (a-B), parseint (a+b));
02-javascript syntax