current position:Home>[JavaScript writing position of the front end, definition variables, basic data types, basic syntax knowledge of data type conversion]

[JavaScript writing position of the front end, definition variables, basic data types, basic syntax knowledge of data type conversion]

2022-01-26 22:09:59 Xiao Yan is the queen

Front end JavaScript Writing position 、 Defining variables 、 Basic data type 、 Basic syntax knowledge of data type conversion

Our common Web The front-end big three :HTML、CSS、JS(JavaScript).
Please move to 【 My column 】 Middle front end ——> Front end HTML+CSS;
Here I will introduce with pictures and texts JS stay Vscode Application of relevant functions in , I hope it can help you .

  1. JavaScript Writing position
    Inline
    embedded
    Outer chain
  2. JavaScript Defining variables
  3. Basic data type
  4. Data type conversion
  5. summary

1.JavaScript Writing position

  • Inline : Write the code directly on the label .
    among a label : Written in href Attribute . The code is as follows :
 <a href="javascript: alert(' Xiao Yan is a royal goddess. Welcome ') ;"> click , Xiao Yan is the imperial goddess </a>

Instead of a label : Write on behavioral attributes . The code is as follows :

<div onclick=" alert(' Xiao Yan is a royal goddess. Welcome ') "> click , Xiao Yan is the imperial goddess </div>

The effect displayed on the browser side is the same , As shown in the figure below :
 Insert picture description here

  • embedded : Write the code in a script Label alignment . embedded JS Code , You don't need to rely on any behavior , Open the page and execute .
    Write specific examples , The code is as follows :
 <script>
 alert(' Xiao Yan is the imperial goddess ');
 </script>

The effect displayed on the browsing side , As shown in the figure below :
 Insert picture description here

  • Outer chain : Write the code in a .js In the file .

Outer chain JS Code , You don't need to rely on any behavior , Open the page and execute , Use script Labeled src Property to specify JS file .

Write specific examples , First create an external chain test.js file , Secondly, the introduction code is as follows :

<script src="./test.js"></script>

Be careful : We usually use the outer chain .

2.JavaScript Defining variables

Definition of variables : While the program is running , Save an intermediate value and use

Variable writing : var num = 100( Meaning : Define a container var And name the container num, Give again num The assignment is 100)

Be careful : A variable can hold only one value .

Writing specification of variables

  • Variables can only be Numbers 、 Letter 、 Underline 、 The dollar sign makes up .
  • Variables cannot start with numbers .
  • Case sensitive .
  • Cannot use keyword .
  • Try to use some meaningful words or Pinyin .

Write specific examples , The code is as follows :

 <script>
 //  Define a variable  num,  The assignment is  100
 var num = 100
 //  In the browser pop-up window  num  The value of the variable 
 alert(num)
 //  Print on console  num  Value 
 console.log(num)
 //  Output directly on the page  num  Value 
 document.write(num)
 </script>

The effect displayed on the browsing side , As shown in the figure below :
 Insert picture description here

3. Basic data type

  • value type Number: Decimal number 、 Scientific enumeration 、 Numbers in other hexadecimal representations (var n1 = 100)
  • String type String: Everything wrapped in single quotation marks or double quotation marks .(var s1 = ‘hello world’)
  • Boolean type Boolean:true and false(var b1 = true It means really )
  • Empty type Undefined:udnefined(var k2 No value )
  • Empty type Null:null(var k1 = null It's worth it , There is a null value )
  • Be careful :
    Use typeof Keyword to detect the basic data type .
    grammar : typeof Variables to detect
    result : The data type of the data stored in this variable

The code for detecting the data type and the effect displayed in the console are shown in the figure below :
 Insert picture description here

4. Data type conversion

【 Conversion value : Convert other data types to value type 】

  • Number();

grammar : Number( Content to convert )

var s1 = '100';var n1 = Number(s1);

result : Convert the result of numeric type 100

  • parseInt();

grammar : parseInt( Content to convert )

var s1 = '100abc' ; var n1 = parseInt(s1);

result : Convert the result of numeric type 100

  • parseFloat();

grammar : parseFloat( Content to convert )

var s1 = '100.12d'; var n1 = parseFloat(s1);

result : Convert the result of numeric type 100.12

【 Turn the string : Convert other data types to String type 】

  • String();

grammar : String( Content to convert )

var b1 = true; var s1 = String(b1);

result : Convert the result of numeric type true

  • toString();

grammar : Content to convert .toString()

var b1 = true; var s1 = b1.toString();

result : Convert the result of numeric type true

【 Turn bull : Convert other data types to Boolean type 】

  • Boolean();

grammar : Content to convert .Boolean()

var s1 = 'abc'; var b1 = Boolean(s1);

result : Convert the result of numeric type true

Be careful : Only 0 、NaN、 ‘’ 、undefined 、null The result is False The rest of the results are true.

5. summary

  • JavaScript Writing position : Inline 、 embedded 、 Outer chain
  • JavaScript Defining variables :var num = 100( Definition 、 Writing 、 standard )
  • Basic data type : value type Number、 String type String、 Boolean type Boolean、 Empty type Undefined、 Empty type Null
  • Data type conversion :Number();parseInt();parseFloat(); 、String();toString();、Boolean();

Okay , About the front end JavaScript Writing position 、 Defining variables 、 Basic data type 、 The basic syntax knowledge of data type conversion has been written very clearly , If you have any problems in actual combat, you can leave a message or send a private letter , I'll help you solve , See you in other articles .

copyright notice
author[Xiao Yan is the queen],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262209560034.html

Random recommended