JavaScript Basic concepts

JS The composition of :

1.ECMAScript:Js grammar

2.DOM: Document object model , file :html file , operation html file ( operation html Documentation tools )

3.BOM: Browser object model , Tools for operating browsers

JS Three ways of writing

Inline , embedded , External links

JavaScript Input · Output statement

alert(msg) A warning box pops up in the browser

consle.log(msg) Browser console printouts

prompt(info) Browser pop-up input box Users can enter information

Concept of variables

popular : Variables are containers for storing data . We go through Variable name get data , Even the data can be modified .

Variable understanding : The amount of change , The content of the change , Changing data

Variable storage data understanding : The data in the process of program running is stored in memory , But the memory is large , For the convenience of management , So divide the memory into boxes ( Variable ) To store

Use of variables

1. Declare variables var Variable name ;var age;( Make a statement The name is age The variable of )

2. The assignment of a variable var Variable name = value ; var age = 18; Declare variables age At the same time, it is assigned to 18

Variable case

var myname = ' Flag wood Kakashi '; //  character string
var address = ' Huoying Village ';
var age = 30; //  Numbers
var email = 'kakaxi@itcast.cn';
var gz = 2000;
console.log(myname);
console.log(address);
console.log(age);
console.log(email);
console.log(‘gz’); // gz
console.log(gz); // 2000

Be careful :

  • When variables are assigned , If the value is a string, you need to add ’’,“”
  • At the end of each line of code , You need to add ;
  • When variables are used , You can't add quotation marks ( Definition , When declaring variables , No quotes , You can't add quotation marks when using it )
    Case study 2:
// 1.  The user enters the name   Store in a  myname In the variables of
var myname = prompt(' Please enter your name ');
// prompt  Do the things :
// (1).  Pop up the input box  ,  User input content :zs
// (2).  Return user input to  , amount to  :var myname = 'zs';
// 2.  Output this user name
alert(myname); // zs

Variable naming conventions

The rules

By letter (A-Za-z)、 Numbers (0-9)、 Underline (_)、 Dollar symbol ( $ ) form , Such as :usrAge, num01, _name ***

Case sensitive .var app; and var App; It's two variables

You can't Start with a number . 18age It's wrong.

You can't Is the key word 、 Reserved words . for example :var、for、while

Variable names must have meaning . MMD BBD nl → age

Follow the hump nomenclature . Initial lowercase , The first letter of the following words needs to be capitalized . myFirstName

Recommended translation website : youdao ICIBA

Variable case - Exchange two variables

// js  It's programming languages that have a lot of logic in them :  The idea of realizing this requirement   What to do first and then what to do  
// 1.  We need a temporary variable to help us
// 2.  hold apple1  Temporary variables for us  temp 
// 3.  hold apple2  The apples in it are for  apple1 
// 4.  Put the value in the temporary variable   to  apple2 
var temp; //  A temporary variable is declared empty
var apple1 = ' green apple ';
var apple2 = ' Red apple ';
temp = apple1; //  Give the right to the left
apple1 = apple2;
apple2 = temp;
console.log(apple1);
console.log(apple2);

Simple data type
JavaScript The simple data types in and their descriptions are as follows :

JavaScript Basic knowledge and writing



Digital range :JavaScript The maximum and minimum of the values in

alert(Number.MAX_VALUE); // 1.7976931348623157e+308 (10 Positive of 308 Power )
alert(Number.MIN_VALUE); // 5e-324 5e-324 (10 Negative 324 Power )

Three special values of digital type

alert(Infinity); // Infinity
alert(-Infinity); // -Infinity
alert(NaN); // NaN ***
// Class code :
// 5.  infinity
console.log(Number.MAX_VALUE * 2); // Infinity  infinity  
// 6.  An infinitesimal
console.log(-Number.MAX_VALUE * 2); // -Infinity  infinity
// 7.  The digital
console.log('pink teacher ' - 100); // NaN: A string cannot operate on a number , So the result is a non number 
  • Infinity , For infinity , Greater than any number
  • -Infinity , For infinitesimal , Less than any number
  • NaN ,Not a number, Represents a non numerical value

isNaN
Used to determine whether a variable is a non numeric type , return true perhaps false

var usrAge = 21;
var isOk = isNaN(userAge);
console.log(isNum); // false ,21  It's not a non number
var usrName = "andy";
console.log(isNaN(userName)); // true ,"andy" It's a non number 

String type String
String type can be any text in quotation marks , Its grammar is Double quotes “” and Single quotation marks ’’

var strMsg = " I love tian 'anmen square in Beijing ~"; //  Use double quotation marks to represent strings
var strMsg2 = ' I love pig's feet ~'; //  Use single quotation marks to represent strings
//  Common mistakes
var strMsg3 =  I love big elbows ; //  Report errors , No quotation marks , Would be considered to be js Code , but js Without these grammars 

String quotes nested
JS You can use single quotes to nest double quotes , Or use double quotes to nest single quotes ( Double outside and single inside , Single outside and double inside )

var strMsg = ' I am a " Gao Shuai Fu " Program the ape '; //  It can be used '' contain ""
var strMsg2 = " I am a ' Gao Shuai Fu ' Program the ape "; //  It can also be used. ""  contain ''
//  Common mistakes 
var badQuotes = 'What on earth?"; //  Report errors , You can't   Single and double quotation marks 

String escape character

similar HTML The special characters in it , There are also special characters in strings , We call it the escape character .

All the escape characters are \ At the beginning , The common escape characters and their descriptions are as follows :

JavaScript Basic knowledge and writing


String length

  • A string is made up of several characters , The number of these characters is the length of the string
  • Through strings  length  Property to get the length of the entire string
var strMsg = " I'm a handsome and golden procedural ape !";
 alert(strMsg.length); //  Show  11

String splicing

  • You can use... Between multiple strings + Splicing , The splicing method is character string + Any kind of = The new string after splicing
  • Any type added to the string will be converted into a string before splicing , And then splice it into a new string
//1.1  character string  " Add up "
alert('hello' + ' ' + 'world'); // hello world
//1.2  Numeric strings  " Add up "
alert('100' + '100'); // 100100
//1.3  Numeric strings  +  The number
alert('11' + 12); // 1112 
// None of the above string splicing makes sense , Generally, we splice strings with variables 

Link to the original text :http://cq.mobiletrain.org/