javaScript variables and keywords

Basic noun concept

Literal

Digital area quantity

Numbers are divided into three types in js: decimal, octal, and hexadecimal

example
alert(52)  // All the numbers we see in the browser are decimal
alert(015) // An octal value starting with 0 will be converted to hexadecimal by the browser
alert(0x18) // Hexadecimal numeric representation
alert(0.45) // Small numbers, also known as floating-point numbers, have no decimal system
alert(5e4) // The index e represents the power of 10, which is equivalent to moving the decimal point back 4 places, 50000
//Infinity 
console.log(3.15e314160998)  //Print infinty different browser sizes
//NAN is not a number, but it is a number type, which is not a legal value
console.log(0 / 0) //The result is nan. No error will be reported for illegal values

Literal amount

Words or numbers enclosed in quotation marks are called literal values

alert("This is text") //You can use single quotation marks or double quotation marks
alert("This is'written words'ah")  //Can be nested
//If you have to use single or double sets, you can use escape characters
alert("Mr. Lu Yao:\"I'm so handsome\"Ha ha ha")   // \Escape characters make the next thing meaningless
•Expansion. \t Space \n Line feed

sentence

JavaScript statements are executed in a single line from top to bottom; Each line is a statement. After each statement is executed, it will enter the next line; ending

Note: JS is run by JS parsing in the browser (parsing before running). There is only one thread, that is, JS is single threaded and shares one thread with the UI thread.

let str = 'hello world!';
console.log(str);

expression

In the above code, 1 + 1 is an expression, which refers to a calculation formula to get the return value.

let num = 1 + 1;

Statement can be understood as a command and does not necessarily need to a specific expected value. The purpose of the expression is to obtain a value to facilitate what to do with the value later. In the above example, the value obtained by expression 1 + 1 is assigned to the variable num;

variable

In the above example, Num is the variable declared by us. The variable can be understood as a container for storing various values. Because we need to store various values, we want to name the variable num, which is the variable name tip in the above example 🐤: Note that js is a loosely typed language, and variables can be loaded with anything

  • A variable is essentially a value with a name

  • A variable is a container in js that is used to load some data

  • Features: whenever code uses this variable, it will use the data in the variable container

Variable declaration / let

In the above example, the value obtained by the expression 1 + 1 is assigned to the variable str declared by the let keyword; The value stored in the memory address pointed to by the subsequent num is the result of 1 + 1. In this process, we actually did two things:

  1. We create variables through the keyword num

  2. We assign the value of expression 1 + 1 to the variable num

// The declared variable is named num and has no assignment. The default value is undefined. Undefined is also a JavaScript keyword, indicating "undefined".
let num;
// In this step, the result of 1 + 1 is assigned to the variable num, so that the content stored in the memory address represented by 1 + 1 is the value of 1 + 1. Later, we want to use the result of 1 + 1, which can be called directly by calling the variable num
num = 1 + 1;

Variable assignment

We can create a variable through the let keyword. The default value of the created variable is undefined. We can store the value of the variable through the assignment expression =. The format is as follows

`Variable name = value`

Variable reference (call)

We learned how to create variables and how to assign values to variables. How do we use the assigned variables?

//Use console The log method prints the value of the num variable in the console
console.log(num);
//Bring the variable num into the expression num + 1, make the expression the content of the num variable + 1, and assign the value to the variable count
let count = num + 1;

Identifier (variable name)

An identifier is a legal name used to identify various values. The most common identifiers are the variable name and the function name to be mentioned later. JavaScript language identifiers are case sensitive, so a and a are two different identifiers.

Naming rules

The variable name must follow the naming convention. If not, an error will be reported

Naming conventions

  • Must be in numbers, letters, underscores_ start

  • It must be composed of numbers, letters and underscores

  • See the name and know the meaning

  • Keywords and reserved words cannot be used

  • JS is strictly case sensitive

Hump nomenclature

A name composed of two words. The first word is lowercase, and the first letter of each word after it should be capitalized. It looks like a hump on a camel, so it is called hump naming method and included in the backgroundColor

Reserved words

rguments,break,case,catch,class,const,continue,debugger,default,delete,do,else,enum,eval,export,extends,false,finally,for,function,if,implements,import,in,instanceof,interface,let,new,null,package,private,protected,public,return,static,super,switch,this,throw,true,try,typeof,var,void,while,with,yield.

Legal identifier

//All legal identifiers in unicode table are OK
num
$con
_target
π
Counter

Illegal identifier

// illegal
1x
321
***
-x
undefined
for

Good naming

maxCount
petName
str
num

Bad name

max-count
maxcount

Rules for variables

Undeclared variables are used directly

console.log(x); 
// ReferenceError: x is not defined

Repeat assignment

let x = 10;
x = 20;
console.log(x); //20
​
Interpreted as 
​
let x;
x = 10;
x = 20;
console.log(x
//`X 'is declared at the beginning and assigned to' 10 '. If you want to modify the value of' x ', you don't need to declare it again. You can directly assign' 20 'to overwrite the previous value of' x ' 

Repeat declaration

let x = 1;
let x = 2;
console.log(x) //Identifier 'x' has already been declared
​
//var before es6 will not have this problem

Batch declaration

let a,b,c,d = 10;
​
Interpreted as
​
let a;
let b;
let c;
let d;
d = 10;
//In the above code, we can separate multiple variables by `, ` and declare them in batch through a ` let 'keyword, and the last variable ` d' is assigned 10;

constant

Constant is a new specifier of es6. Constants are similar to variables, but the literal quantity declared by constants cannot be modified. const has other special properties, which will be studied later

const nice = 10;
nice = 20 // Assignment to constant variable
//Error message constant variable assignment

Keywords: Javascript

Added by wiztek2000 on Tue, 04 Jan 2022 14:01:29 +0200