JavaScript is not sensitive to newlines, indentations, and spaces.
A semicolon must be added at the end of each statement. Although the semicolon is not necessary, in order to compress the program in the future, if the semicolon is not added, it will not run after compression.
All symbols are in English. Such as brackets, quotation marks, and semicolons.
program can make it easier to understand and explain the function of the program, and enhance the readability of the code. The program with the comment content will not run.
Variables are declared using the system keyword var
. Variables can be declared individually or multiple at the same time. Variables can also be initialized (variable character values) when declaring.
For example:
var name; //Declare one Variables var name, sex, age; //Declare multiple variables at the same time, separated by commas in English var name = "Xiaoqiang"; //
Declaration of variables while declaring and assigning values at the same time (three ways)
Variable usage It needs to be declared first; the keyword for declaring a variable is: var;
syntax: var variable name = variable value;
declare first, then assign value
var a; //Declaration of variable a = 10; //Assignment of variabledeclare and assign value
var
at the same time
a = 10; //Declaration and assignment of variables
declare multiple variables and assign values at the same time (note: when declaring multiple variables and assigning values, separate the variables with commas)
var a = 10, b = 20; //Multiple The declaration and assignment of variables
are not declared, but assigned directly (note: this is an irregular way of writing!)
age = 18; //Assignment of variables console.log(age); //The result is 20, and the browser can help identify it. But it is not standardized. It is not recommended to use
only declaration and no assignment. In this case, the default is undefined
var name; //Declaration of variables console.log( name); //undefinedis neither declared nor assigned.
If a variable is not declared
, it will notbe used directly.
Assign a value and use it directly. At this time, an error will be reported, name is not defined, because the name variable does not exist at all. Note: Starting from the line where the error is reported, the subsequent code will not be executed!
console.log( 'I am the first line of code' ); //Normal display console.log( name ); //Error reporting console.log( 'I am the second line of code' ); //Do not display non-executable
are strictly case-sensitive (uppercase variables and lowercase variables are different variables).
Variable names cannot start with numbers, but can start with letters, underscores or $
.For example: var _name, $age; (correct) var 3abc;( Wrong)
variable name cannot be a system keyword.
For example: var, switch, for, case, else, while, etc.
Variable names in JS are case-sensitive.
For example: name and Name are two variables
. The names of variables in JS must be meaningful.
For example: the variable representing age is age, and the name is userName.
When the variable name consists of multiple words, there are two naming methods
: camel case naming:
the first word is all lowercase, and the first letter of each subsequent word is capitalized.
For example:
var userName = "Steel Egg"
Underline naming:
all words are in lowercase, connected with underscores in the middle.
For example:
var get_user_name =
Variables have data types, and JavaScript is a weak type or in other words Dynamic language, which means that there is no need to declare the type of the variable in advance. When the code is running, the data type of the variable is determined by the JS engine based on the data type of the variable value on the right side of the variable = (equal sign). It also means that the same variable can be used as different types. This type comes from the "value of the variable".
In other words: whatever type the value is, the variable is of that type.
The types of variables in JS are:
numeric (number), character (string), boolean (boolean), undefined (undefined), null (empty), array (array), object (object), function (function)
numerical type, character type, Boolean type, undefined type, and null type. Very notable feature: a variable name can only store one value.
var a = 10; var b = 'string'; var c = false; var d = undefined; var e = null;
array, object, function, notable feature: one variable name may store multiple values.
var arr = [10,20,30,40];//Array var today = new Date();//Object//Function function myFunction(){ console.log('function'); }
include: integer type, floating point type, and NaN (indicating that it is not a numerical value).
var a = 999; // Integer type var b = 0.9; // Floating point type var c = NaN; //
A very special value NaN in the NaN numerical type, NaN (not a number) is not a number; when other values are The data type cannot be converted into a numerical type, but the program cannot report an error. In this case, a NaN value will be returned; NaN itself is a Number variable.
is a string enclosed by single quotes or double quotes.
var a = "I am a string"; var b = 'I am also a string'; var c = "";
Note:
Boolean type is also called logical type. There are only two values: true (true), false (false).
Boolean has only two states. Such as: gender, marital status, light switch, etc.
Boolean type commonly used if conditional judgment statement
var a = true; var b = false;
When a variable is defined but not assigned a value, an undefined type will be returned. The value of an undefined type has only one undefined
.
When an object's property does not exist, undefined is also returned.
var a;//The variable is defined but not assigned a value console.log(a) // undefined // An object is defined var obj = { uname: 'PINRU', info: 'You are so sexy! ', age: 20 } var test = obj.gender console.log(test) // undefined
When an object does not exist, the null type will be returned, and the value of the null type is only null.
It can also be understood as: a placeholder for an object.
If you want to clear the value of a variable, you can assign a null value.
var a = 100; var a = null; //Assign a null to a variable to clear its value