Variable basis
The keyword
Language built-in for defining syntax: var, function, etc
Reserved words
Keywords to be used in the future: enum, public, etc
identifier
An identifier is the name of a variable, function, attribute, or function parameter.
Naming conventions
- The first character must be a letter, underscore (_), or dollar sign ($);
- The remaining characters can be letters, underscores, dollar signs, or numbers.
- The keyword cannot be used as an identifier
- Case sensitive
Naming habits
- The small hump:
firstName
. Commonly used in variable names, function names, etc - Big hump:
Array
,RegExp
. Commonly used in class libraries, constructors, etc
Literals and variables
- Literal:
123
,'abc'
,56.79
The literal quantities themselves - Variables: Used to hold these literals so that they can be called directly when used.
Variables are like “boxes” that hold real data. Variables exist for the convenience of manipulating real data
Variables and Constants
- Variable: Something that changes, something that can be reassigned.
Key words: VAR, let(ES6)
- Constant: A fixed quantity that must be assigned once defined and cannot be changed again
Keyword: const(es6)
Declaration of variables
var a;
Declare multiple variables separated by commas
var name,
age,
sex;
Copy the code
Assignment of a variable
Js variables are loosely typed, meaning that variables can be used to hold any type of data. Also known as dynamic weakly typed JS variables
var msg = 'Hello JavaScript';
msg = 123
Copy the code
Variable declaration enhancement
Variables declared by the keyword are automatically promoted to the top of the current scope
console.log(a) // undefined
var a = 'a'
Copy the code
The above code is equivalent to
var a
console.log(a) // undefined
a = 'a'
Copy the code
Declaring the same variable more than once will not report an error
var a = 'a'
var a = 'aa'
var a = 'aaa'
console.log(a); // aaa
Copy the code
practice
1. Prompt receives variables
// prompt(prompt text, default value) returns the input value
var msg = prompt('Please enter a value:'.'123')
console.log(msg);
Copy the code
Swap the values of the two variables
var a = 123
var b = 321
// Swap a and b
var temp
temp = a
a = b
b = temp
console.log(a, b);
Copy the code
Introduction to variable types
Simple data types (basic data types)
Numerical (Number)
All numbers are stored as floating-point numbers, even integers.
There are no integers at all at the bottom of the JS language, all numbers are decimals
String (String)
Used to represent a sequence of zero or more characters, that is, a string
- Strings need to be wrapped in single or double quotation marks
- String literals can be concatenated with a hyphen + and a variable
Boolean value (Boolean)
Boolean values represent both true and false states. “True” is represented by the keyword true, “false” is represented by the keyword false. Booleans have only these two values.
- Note: is case sensitive
Undefined (undefined
)
- The uninitialized variable is
undefined
You don’t have to explicitly assign it toundefined
- To distinguish between empty objects
null
Of an uninitialized variable
An empty object (Null).
- Variables to be saved as objects are best initialized to NULL
Complex data types
Array (Array)
Is a set of values arranged in order. Each value position is numbered (starting at 0), and the entire array is represented by square brackets.
Object in a narrow sense
An object is a set of key-value pairs, a kind of unordered composite data set.
All of the key names of the object are strings, so you can omit quotes (do not treat key strings that omit quotes as variables).
If the key name does not meet the criteria for identifying a name (for example, the first character is a number, or contains a space or operator) and is not a number, it must be quoted; otherwise, an error will be reported.
Each key name of an object is also called a property, and its “key value” can be any data type.
Function (Function)
Holds executable code data that functions need to be called to execute the code inside the function
There are three ways to declare
- The function command
- Functional expression
- Function constructor (more on that later)
typeof
- Is an operator that determines the type of a variable
- typeof(a)
- typeof a
- Return result:
- “Undefined” — if the value is not declared or assigned;
- “Boolean” — if the value is a Boolean;
- “String” — if the value is a string;
- “Number” — if the value is numeric;
- “Object” — if the value is an object or null(empty object);
- “Function” — if the value is a function.
The stack and heap
- Simple data is stored directly in stack memory
- Complex data is stored in heap memory, and addresses in heap memory are stored in stack memory
Graph RL subgraph stack G[#101] F[#100] C[true] B['hello'] A[123] end Subgraph heap E["#101 -- {name: 'xiaoming'} "] -- > G D [# 100 - "[' a ', 'b', 'c']"] -- > F end
Nesting of variables
If an attribute in an object has a function value, it is usually called a “method” and can be called like a function.
practice
Printing User Information
- Enter your name and age in the input box that pops up
- After receiving with variables respectively
- Hold several hobbies in arrays
- Print some behavior with a function
- Put the user information on an object
- Find each property through the object and print the following:
XXX is x years old. Like reading, coding, listening to music, X is eating!
var name = prompt(May I have your name, please ')
var age = prompt(Whats your age, please ')
var likes = ['read'.'Knock code'.'Listen to music']
var eat = function () {
console.log(name + 'Eating! ');
}
var info = {
name: name,
age: age,
likes: likes,
eat: eat
}
console.log(info.name + 'this year' + age + 'years old. Like the ' + info.likes[0] + ', ' + info.likes[1] + ', ' + info.likes[2] + ', ')
info.eat()
Copy the code