The first JavaScript code

Source: Internet
Author: User
Tags naming convention script tag string to number

Since our CSS has to be placed in a special style tag, JavaScript also needs to be placed in the child's tag, which is the script tag

In the page, we can put tag pairs in the body tag <script type=”text/javascript”></script> , <script type=”text/javascript”></script> tag pairs

We can define it directly in the body or in heard, but the general definition is in the heard.

<script type= "Text/javascript" ></script>

Alert statement:

We can understand that bit is JavaScript output box because different languages have different output this can understand bit JavaScript output

    <script type= "Text/javascript" >        alert (' Big Brother ');     </script>

The purpose of alert (translated as "alert"): pops up "warning box".

Syntax rules: Names of variables:

JavaScript defines variables that need to be added to Var before the variable

var a = 666;

This is the same as the explanatory language. Defining variables is preceded by some particular type of variable you want to define or the specified situation

Defining variables is usually defined by Var let to define variables in front of the variable plus var

// defining variables A and variable b var a = 3= 4
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.

Learning programs are regularly followed by programs that have the same parts of these parts that are a rule that cannot be changed, which we call grammar.

JavaScript is different from Python, but is distinguished by {} In other languages, and the end of it is dependent on the semicolon.

1) JavaScript is not sensitive to line, indent, or 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.

3) JavaScript Comments:

Single-line Comment:

I'm a note.

Multi-line Comments:

/* Multiline    Comment 1 multiline    comment 2*/

Note: In sublime or Pycharm, the shortcut key for a single-line comment is ctrl+/ that the shortcut key for a multiline comment is ctrl+shift+/ .

JavaScript output information in Web page popup warning Box: Alert ("") Console output: Console.log ("")

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.

ES6 syntax template string ' TAB key above the key interpolation variable using the ${variable name}
Console.log (' ${first} Love ${second} You ');

Console.log (parseint ("2018) you are so handsome!! "));

(2) All the symbols are in English. such as parentheses , quotation marks, semicolons.

User input: Prompt () statement

In the prompt () statement, the user is a string regardless of what is entered.

prompt()is specifically used to pop up a dialog box that allows the user to enter.

He has the same effect as input in Python.

Here's the way.

The code is as follows:

    <script type= "Text/javascript" >        = prompt ();        Console.log (a);     </script>

Then you can see the information you entered in the console of your F12.

JavaScript inline add-in style:

    <title>Document</title>    <style>        div{            height:100px;            width:100px;            Background-color:red;        }     </style>

Effect:

When you click on the red block generated by the Div, a pop-up box appears

The external style is the style written in the Heard,

    <title>Document</title>    <style>        div{            height:100px;            width:100px;            Background-color:red;        }     </style>    <script type= "Text/javascript" >        alert (111);     </script>

The effect is the same, you have to define a script in heard to define a label that can receive content from JavaScript

The difference between alert and prompt:

Alert ("Formerly a Mountain");                Direct use, do not need var a = prompt ("Please enter a number");   A variable must be used to receive the value entered by the user
Direct Volume: Numbers and strings

A "direct quantity" is a constant , also known as a literal. See what, it is what.

There are 2 simple direct quantities: numbers, strings.

(1) The direct amount of the value of the expression is very simple, write up on the line, do not need any symbols. For example:

alert (886);  886 is a number, so no quotation marks are required.

(2) The string is also simple, but be sure to enclose it in quotation marks. It can be words, sentences and so on.

Types of variables

Variables can store numbers, strings, and so on. Variables automatically determine their type based on the type of stored content.

Numeric type number:

If a variable holds a number, then the variable is numeric.

<script type= "Text/javascript" >
Alert (111);
Let A = 345;
Console.log (typeof a);

</script>

Console viewed from F12

typeof () indicates

It and Python look for data type Chabudu is

typeof () means " get the type of the variable " and the syntax is:

typeof variable

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.

String Type: String
<script type= "Text/javascript" >Alert (111); Let a= 345; Console.log (typeofa); LEC a= "ABCDE"; varb = ' Hello '; varE = ' hahaha '; Consloe.log (typeofA, B, c); //Console.log (typeof b);        //Console.log (typeof c);    //Console.log () cannot print all outputs that must be once at a time</script>

The difference between hyphenation Fuhai

The keyboard + may be a hyphen or a plus sign of a number. As follows:

   Console.log ("I" + "Love" + "You");   Hyphen, put three separate kanji, connected together    console.log ("I + Love + You");           Output    Console.log (1+2+3) as-is;             Output 6

Summary : If both sides of the plus sign are numeric, this is the plus. Otherwise, it is a hyphen (used to concatenate strings).

Transfer of variable value (Assignment)

Statement:

   A = b;

Assigns the value of B to a A, a, or a.

Assigns the value to the right of the equal sign to the left variable, the variable to the right of the equal sign, and the value unchanged.

To give a special example:

        var a = "3";        var b = 2;        Console.log (A-B);

Effect: (Note, string-numeric = numeric)

Variable format to convert user input

What we've said in the above is that it prompt() 's designed to pop up a dialog box that lets the user enter. The important thing is that the user is a string regardless of what they enter.

parseInt(): String to Number

parseint () to convert a string to a number . Parse represents "transform", and int denotes "integer" (note Int the spelling). For example:

String to number method:

parseint ("5");

parseint () also has the following characteristics :

(1) With the function of automatic purification, only the number of the beginning of the string is preserved , the following Chinese automatically disappears. For example:

Console.log (parseint ("2018) you are so handsome!! ");

(2) Automatically with truncation decimal function: Rounding , not rounding .

var a = parseint (5.8) + parseint (4.7); Console.log (a);
var a = parseint (5.8 + 4.7); Console.log (a);

The first JavaScript code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.