Chapter 3 Basic concepts

A, grammar,

Identifier: The name of a variable, function, attribute, or function argument. Rules:

  • The first character must be a letter, underscore (_) or a dollar sign ($)
  • Other characters can be letters, underscores, dollar signs, or numbers

You cannot use keywords, reserved words, true, false, and NULL as identifiers.

Strict Patterns: ECMAScript 5 introduces strict patterns, which define a different parsing and execution model for JavaScript. In strict mode, uncertain behavior in ECMAScript 3 will be handled, and errors will be thrown for unsafe operations.

// Enable strict mode at the top of js
"use strict";

// Specify the strict mode in the function
function doSomething(){ 
"use strict";
}
Copy the code

Keywords and reserved words

Keywords: have a specific purpose, can be used to control the start or end of a statement, or to perform a specific operation, etc. Keywords are also language-reserved and cannot be used as identifiers.

break do instanceof typeof case else new var catch finally return void continue for switch while debugger* function this with default if throw delete in try
Copy the code

Reserved words: do not have any specific purpose yet, but may be used as keywords in the future; Cannot be used as an identifier.

abstract enum int short boolean export interface static byte extends long super char final native synchronized class float package throws const goto private transient debugger implements protected volatile double import public
Copy the code

Three, variables,

Loosely typed: Can be used to hold any type of data, and each variable is just a placeholder to hold a value.

var message;// Uninitialized variables will hold a special value -- 'undefined'
var message = "hi";// Initializing a variable does not mark it as a string, only as an assignment

function test(){ 
    var message = "hi"; // Local variables are destroyed after the function exits
} 
test(); 
alert(message); / / error!

function test(){
    message = "hi"; // Global variables, once called 'test()', are defined and can be accessed from anywhere outside the function. A 'ReferenceError' error is thrown in strict mode.
}
test();
alert(message); // "hi"
Copy the code

4. Data types

  • Five simple data types:Undefined,Null,Boolean,NumberandString.
  • 1 complex data type:Object

Typeof operator: detects the data typeof a given variable

  • "undefined"— If the value is undefined
  • "boolean"If the value is a Boolean
  • "string"If the value is a string
  • "number"If the value is a number
  • "object"If the value is an object ornull
  • "function"If the value is a function
typeof 'message' // "string" 
typeof('message') // "string"
`typeof null` // "object", 'null' is considered an empty object reference
Copy the code
1.Undefined

Only variables are declared, but not initialized.

var message; // This variable declaration takes undefined by default
var age // This variable is not declared
alert(typeof message);     // "undefined"
alert(typeof age);         // "undefined" 
Copy the code
2,Null

Represents a null object pointer whose undefined value is derived from null

alert(null= =undefined); //true, the operator converts its operands for comparison purposes
Copy the code
3,Boolean

Although Boolean types have only two literals: true and false, all types of values in ECMAScript have values equivalent to these two Boolean values. To convert a value to its corresponding Boolean, call the transformation function Boolean()

4,Number
  • Floating-point values: Values must contain a decimal point, and since saving floating-point values requires twice as much memory as saving integer values, ECMAScript loses no time converting floating-point values to integer values.
var floatNum1 = 1.; // There is no number after the decimal point -- resolves to 1
var floatNum2 = 10.0; // Integer -- resolves to 10
Copy the code

For values that are extremely large or extremely small, floating point values can be expressed in the e notation (scientific notation). ECMAScript converts floating point values with more than six zeros after the decimal point to the e representation.

var floatNum = 3.125 e7; / / equal to 31250000
var floatNum = 3e-17 / / is equal to 0.00000000000000003
Copy the code

The minimum value ECMAScript can represent is stored in number.min_value and the maximum value is stored in number.max_value. Values out of range are automatically converted to special Infinity values. (-infinity, Infinity)

// use the 'isFinite()' function to determine if a number isFinite
isFinite(Number.MAX_VALUE + Number.MAX_VALUE) //false
Copy the code
  • NaN: Not a Number is a special Number; Any operation that involves NaN (such as NaN/10) returns NaN, and NaN is not equal to any value, including NaN itself.
// isNaN() checks if "not a number"
alert(isNaN(NaN)); //true 
alert(isNaN(10)); //false (10 is a number)
alert(isNaN("10")); //false (can be converted to value 10)
alert(isNaN("blue")); //true (cannot be converted to a value)
alert(isNaN(true)); //false (can be converted to value 1)
Copy the code
  • Numerical transformation:Number(),parseInt()andparseFloat()
5,String

Represents a sequence of zero or more 16-bit Unicode characters, known as strings. ToString () converts a value to a string.

Values, booleans, objects, and string values all have toString() methods. Null and undefined do not have this method. You can also use the String() function if you do not know whether the value to be converted is null or undefined.

6,Object
  • Constructor: holds the function used to create the current Object. The constructor is Object().
  • hasOwnProperty(propertyName): checks whether a given property exists in the current object instance (rather than in the instance’s prototype).
  • isPrototypeOf(object): used to check whether an object passed in is a prototype of another object.
  • propertyIsEnumerable(propertyName): checks whether a given attribute can be enumerated using a for-in statement.
  • toLocaleString(): Returns a string representation of the object corresponding to the locale of the execution environment.
  • toString(): Returns a string representation of an object.
  • valueOf(): Returns a string, numeric, or Boolean representation of an object. Usually withtoString()Method returns the same value.