Naming rules for js identifiers, javascript markup language

Hello everyone, would you like to share with you what are the naming rules for js identifiers? Try to sum it up, many people still don’t know this. Let’s explain it in detail below. Now let’s take a look!

Comment

// 单行注释 快捷键:ctrl+/
/**
 * 多行注释
 * 快捷键:shift+alt+A
 */

identifier

Identifiers refer to names of variables, arrays, functions, etc. Defining an identifier is the process of taking a name . Deepl reduces weight .

Naming rules for identifiers:

Hard requirements:

  • Numbers, letters, underscores, $
  • Cannot start with a pure number or digit
  • Strictly case sensitive

Soft requirements: know the meaning by seeing the name, no keywords, reserved words, etc. can be used

How to name identifiers:

Snake nomenclature, connected by underscores: for example - login_id

Camel case nomenclature: 1. Small hump: for example - loginId; 2. Big hump: LoginId

variable

A variable is a container that stores data, and the data inside can change at any time.

where (ES5) 

1. No block-level scope

2. Statements can be omitted

3. Variables can be improved.

It can be used before declaration, but it prints out as undefined.

let(ES6)

Declare ordinary variables. If the variable is declared without assigning a value, it is undefined;

Let cannot declare variables repeatedly, but they can be reassigned and cannot be used before declaration.

let has block-level scope: {let a = 10}, at this time a can only be used inside curly braces

Multiple variables can be declared at one time, separated by commas, similar to the union selector in CSS.

constant

const(ES6)

characteristic:

  1. For simple values, they cannot be changed;
  2. For complex values, they can be modified without changing the reference;
  3. Variables declared as const must be assigned a value.

type of data

Every value in JS must belong to a certain data type. Use typeof ----- to check the data type.

Basic data types

Basic data types are also called: primitive data types || simple values

Basic data types illustrate default value
undefined undefined undefined
null null null
number number 0
boolean Boolean false
string string " "
symbol(ES6) symbol

numeric type number

1. Integer

let a = 10;

2. Floating point numbers

let b = 1.2;
let c = 1.3;
let d = b + c;

3. Maximum value and minimum value

console.log(Number.MAX_VALUE);
console.log(Number.MAX_VALUE);

4, Mugidai

console.log(Number.MAX_VALUE*2);// 会得到 Infinity 无穷大

5. NaN = not a number

Features:

  1. A non-number is not equal to any value, including itself;
  2. Its data type is number;
  3. Calculations involving NaN will only yield NaN.

String type

Single quotes'' (recommended)

Double quotes""

``ES6’s new string template features: 1. Support multi-line text; 2. Perform variable parsing

1. Pay attention to the nesting of quotation marks. You can use different quotation marks to distinguish them.

let str = '他说:“好好学习天天向上!”';
console.log(str);

2. The escape character \ is written in front of the symbol

let str2 = '他说:\'好好学习天天向上!\'';
console.log(str2);

3. Line break\n = newline

let str3 = '好好学习,\n天天向上';
console.log(str3);

4. Check the length of the string

let str4 = '好好学习 天天向上'; // 长度为9,一个空格字符也算一个长度
console.log(str4.length);

5. String splicing+, strings and any value splicing will be converted into strings

console.log('好好学习'+'天天向上');
console.log('学习'+666); 

6. ES6’s new string template ``: you can enter text in it, and the input variables need to be wrapped with ${}

Features:

1. Support multi-line text;

2. Perform variable analysis;

let str5 = `0糖
0卡
0脂肪`;
console.log(str5);
let age = 18;
console.log(`我今年${age-2}岁`);// 打印 我今年16岁,可以直接在{}里面进行运算

boolean type boolean

Boolean true (true) and false (false), any data type can be converted to boolean type.

Will return false in boolean:

  • Empty string, including '', "", string template``

  • 0

  • NaN

  • false

  • null

  • undefined

undefined undefined

Undefined: undefined occurs when no value is assigned when declared.

Will return undefined:

  • Access unassigned variables ----- Example: let a;

  • Any variable that is set to an undefined value ----- Example: let a = undefined;

  • A function without a return expression will implicitly return undefined ----- Example: function foo(){}

  • The return expression returns nothing explicitly ----- Example: function foo(){ return;}

  • Access non-existent attributes ----- Example: zhangsan.girlfriend

null value null

let space = null;
console.log(null == undefined);// true
console.log(typeof space);// 查看null的数据类型时得到的是 object
let a1 = '10',a2 = 10;
console.log(a1==a2);//返回true ,不精确比较:只比较值,不比较数据类型
console.log(a1===a2);//返回false, 精确比较:值和数据类型都一样才返回true
console.log(null+'aaa');// 返回nullaaa
console.log(null+1);// 返回1

The difference between undefined and null:

undefined means missing value;

null is usually used to free memory and is treated as 0 during calculations.

Reference data type

Reference data types are also called synthetic data types || complex values

  • object object
Data type conversion

Type conversion is divided into: implicit conversion and explicit conversion.

1. Implicit conversion:

It is a conversion that occurs automatically within the system, generally when operations are performed on different data types:

  • Compare different data types

  • Perform operations on different data types

  • Evaluate Boolean values ​​for non-Boolean values

2. Forced conversion:

If you don't want a number to change, you can let it participate in calculations, -0, *1, etc., or you can add ~~ in front to convert it into a number.

Forced conversions are divided into three major categories: the sample code is as follows:

// 一:转换成字符串
// 1、转换成字符串型 变量.toString()
let num = 10;
let str6 = num.toString();
console.log(typeof str6);

// 2、强制转换 String()
console.log(String(num));

// 3、利用 + 做字符串拼接转换,和字符串拼接的结果都是字符串
console.log(111+'');// 转换成功,打印为111字符串


// 二:转换成数字型
// 1、parseInt()  转换成整数 遇到了第一个非数字时就返回NaN,遇到了一个其他字符就返回前面的整数
let age1 = '18';
console.log(parseInt(age1)); // 打印18
console.log(parseInt(3.14));// 打印3
console.log(parseInt('314abc'))// 打印314
console.log(parseInt('abc314')); // 打印 NaN

//2、parseFloat()   转换成浮点数 遇到了第一个非数字时就返回NaN,遇到了一个其他字符就返回前面的小数
console.log(parseFloat('3.14'));// 打印3.14
console.log(parseFloat('3.a4'));// 打印3
console.log(parseFloat('3.14abc'));// 打印3.14
console.log(parseFloat('abc3.14'));// 打印NaN
console.log(parseFloat('3.14.256a'));// 打印3.14
console.log(parseFloat('3.1a4.256'));// 打印3.1

//3、Number()  这个方法做整体转换,只要有其他字符则返回 NaN
let str7 = '123';
console.log(Number(str7));
console.log(Number(123.4));// 打印123.4
console.log(Number('123.4'));// 打印123.4
console.log(Number('123.4a'));// 打印NaN
console.log(Number('abc123.4a'));// 打印NaN

//4、利用算数运算转换  - * / 注意:没有+运算,因为+是字符串拼接
console.log('2' * 5);// 打印10
console.log('2' * '5');// 打印10
console.log('10' / 5);// 打印2
console.log('2' - 5);// 打印-3


// 三:转换成布尔型
console.log(Boolean(0));// 打印false
console.log(Boolean(' '));// 打印true
console.log(Boolean('0'));// 打印true
console.log(Boolean(null));// 打印false
console.log(!!null);// 打印false
console.log(Boolean(undefined));// 打印false
console.log(Boolean(NaN));// 打印false

.toFixed() method

Write the decimal places in parentheses:

Feature 1: The result count is of string type.

Feature 2: The following digits can be retained.

operator

1. Classification by number of operations

  • Unary operator ----- Example: typeof

  • Binary operator-----Example: */

  • Ternary operator-----Example: ternary operator

2. Classification by usage

arithmetic operators
operator describe Example
+ addition operator x + y means calculating the sum of x plus y
- subtraction operator x - y means calculating the difference of x minus y
* multiplication operator x * y means calculating the product of x times y
/ division operator x / y means calculating the quotient of x divided by y
% Modulo (remainder) operator x % y means calculating the remainder of x divided by y
** power symbol x ** 3 = x to the 3rd power, x ** 4 is equal to x to the 4th power

Increment and decrement operators

The difference between before and after :

++ is in front, it is incremented first and then participates in the operation; ++ is in the back, it is involved in the operation and then it is incremented

-- In the front, decrement first and then participate in the operation; -- In the back, participate in the operation first and then decrement

operator name
++i increment operator Add 1 to i and return the value of i
i++ increment operator Returns the value of i, then increments i by 1
--i Decrement operator Decrement i by 1 and return the value of i
i-- Decrement operator Returns the value of i, then decrements i by 1
assignment operator
operator describe Example
= Assign the value on the right side of the operator to the variable on the left side of the operator x = 10 means assigning the variable x to 10
+= First perform the addition operation, and then assign the result to the variable on the left side of the operator x += y is equivalent to x = x + y
-= First perform the subtraction operation and then assign the result to the variable on the left side of the operator x -= y is equivalent to x = x - y
*= First perform the multiplication operation and then assign the result to the variable on the left side of the operator x *= y is equivalent to x = x * y
/= Perform the division operation first, and then assign the result to the variable on the left side of the operator x /= y is equivalent to x = x / y
%= First perform the modulo operation, and then assign the result to the variable on the left side of the operator x %= y is equivalent to x = x % y
String operators

In addition to performing attribute operations, the + and += operators in JS can also be used to splice strings:

  • +The operator means splicing the strings on the left and right sides of the operator together;

  • +=The operator means to concatenate strings first, and then assign the result to the variable on the left side of the operator.

Relational (comparison) operators

Notice:

1. When comparing characters with characters, compare according to ASCll code.

2. When numbers are present, priority is given to converting them to numbers when comparing.

3. Since strings cannot be converted to numbers, NaN will be returned, so the comparison result will always be false.

operator name Example
> more than the x > y means true if x is greater than y
< less than x < y means true if x is less than y
>= greater or equal to x >= y means true if x is greater than or equal to y
<= less than or equal to x <= y means true if x is less than or equal to y
== equal x == y means true if x equals y
=== All equal (exact comparison) x === y means true if x is equal to y and x and y are also of the same type
!= not equal to x != y means true if x is not equal to y
!== Insufficiency etc. x !== y means true if x is not equal to y, or if x and y are of different types
Logical Operators
operator name Example
&& Logical AND (and) x && y means true if both x and y are true
|| Logical OR (or) x || y means if either x or y is true, then it is true
! logical negation (not) !x means true if x is not true

&& short-circuit: returns the first false value if false, if there is no false value, returns the last true value :

Code example:

console.log(4 && '4');// 打印'4':为真,返回的第二个
console.log(4 && '');// 打印'':
console.log(0 && true);// 打印0:为假,短路,返回第一个
console.log(0 && NaN && 1);// 打印0:为假,短路,返回第一个
console.log(false && NaN && null);// 打印false:第一个为假,则返回假
console.log('abc' && true && 1);// 打印1:都为真,则返回最后一个值

|| Short circuit: Returns the first true value if true, if there is no true value, returns the last false value :

Code example:

console.log(4 || '4');// 打印4,遇真则返回真值
console.log(4 || 'abc');// 打印4,其中有一个为真,则返回真的值
console.log('' || 'abc');// 打印'abc',
console.log(undefined || '');// 打印''
console.log(undefined || null || '');// 打印''
console.log(1 || null || '');// 打印1
console.log(4 > 5 || 2 < 3);// 打印true

! Not (negation): Returns a boolean value: either true or false, or false or true:

Code example:

console.log(!(200 > 201));// 打印true
console.log(!1);// 打印false
console.log(!!1);// 打印true
console.log(!!null);// 打印false
console.log(Boolean(null));// 打印false
ternary operator

Syntax format:

条件表达式1 ? 表达式2 : 表达式3;
// 执行思路:如果表达式1的结果为真,则执行表达式2,如果表达式1的结果为假,则执行表达式3

Guess you like

Origin blog.csdn.net/wenangou/article/details/134497374