Chapter 3 Data Types of Basic Concepts

1 Sorting out small knowledge points:

  1. js is case sensitive
  2. Identifier naming rules are the same as java
  3. There are five data types:
    undefined, Null, Boolean, Number and String

2 typeof operator

Operators (not functions) used to determine the data type , for example:
undefined-if the value is not defined
boolean-if the value is a boolean
string-if the value is a string
number-if the value is a number (it can be shaped or can Floating point)
object-if the value is an object or null
function-if the value is a function

example:

var message="some string";
alert(typeof message);   //"string"
alert(typeof 95);  //"number"
alert(typeof null);//"object,因为null被认为是一个空对象引用"

3 Undefined

There is only one value, undefined, which means a variable that has not been initialized

var message;
alert(message == undefined);//true,因为没有对变量进行声明
var message = undefined;
alert(message == undefined);//true,因为对变量声明的数据类型就是undefined
alert(typeof age);//undefined,因为变量没有进行声明

4 Null

  1. There is also only one value, which is null, which means an empty object pointer.
  2. If the defined variable is going to be used to save the object in the future, it is best to initialize it to null
  3. undefined is derived from the null value, so when judged by null, they are equal
 alert(null==undefined);

5 Boolean

  1. There are two values, and because js is case sensitive, there is only one way of writing true and false, and none of the other mixed capitalizations.
  2. If you want to convert the value to Boolean type, you can use the Boolean function, such as:
var message= 12;
var messageBoolean = Boolean(message);
alert(messageBoolean);
type of data Value converted to true Value converted to false
Boolean true false
Number Any non-zero number 0 sum NaN
String Any non-empty string " "
Undefined Not applicable undefined
Object Any object null

6 Number

  1. For maximum or minimum values, use scientific notation (e notation), for example:
    var num = 3.163e7; //means 31630000

  2. Numerical range:
    If the result of a calculation exceeds the value of the js numerical range, it is automatically converted to Infinity, that is, if it is a negative number, it is converted to -Infinity, if it is a positive number, it is converted to Infinity.
    To determine whether the value is within the js value range, use the isFinite() function.

  3. NaN
    means not a number. (Not a number)
    indicates that any operand that originally intended to return a value does not return a value.
    Any operation involving NaN will return NaN.
    Any value divided by a non-number will return NaN.

After isNaN() receives a value, it will try to convert it to a numeric value. If it can be converted, it will be false, if not, it will be true. For example:

alert(isNaN(NaN));    //true
alert(isNaN(10));    //false
alert(isNaN("10"));   //false(转成10)
alert(isNaN("blue"));  //true(不能转)
alert(isNaN(true));    //false(转换成1)
  1. Numerical conversion
    There are three functions that can convert non-numeric values ​​to numeric values: Number(), parseInt() and parseFloat(). The first one is used for any data type, and the
    latter two are used for string conversion to numeric value.

Number() function conversion rules:

  • Boolean值,true->1,false->0;
  • null value, converted to 0;
  • undefined, return NaN;
  • If it is a string, the leading 0 is ignored (000456 is converted to 456), the empty is converted to 0, the hexadecimal format is converted to the equivalent decimal, and the number is only the number, and the
    others are converted to NaN.

parseInt() function conversion rules (see the first character encountered):
Ignore the spaces in front of the string until the first non-blank character is found. Assuming that the first character is not a number or a negative sign, then NaN is returned, and the leading 0 is also ignored. The beginning of 0 is treated as octal, and the beginning of 0x is treated as hexadecimal. Finally, until a character that is not a number is encountered (stop when it encounters a decimal point, Can be understood as rounding).

E.g:

var num1 = parseInt("1234blue");  //1234
var num2 = parseInt("");    //NaN
var num3 = parseInt("0xA");  //10
var num4 = parseInt("22.5"); //22
var num5 = parseInt("070");   //56
var num6 = parseInt("0xf");   //15

You can provide a second parameter to the function to determine the base number used.
For example:
var num=parseInt("AF",16); //175, hexadecimal is converted to decimal 175

parseFloat:

  • Only parse decimal, hexadecimal is always 0
  • The second decimal point is invalid
  • Leading 0 is always ignored
  • If it can be parsed as an integer, then parse as an integer

7 String

Once the string is created, the value cannot be changed. To change, not only need to delete the original value, but also open up a new space.
For example:

var lang = "Java";
lang = lang + "Script";

First open up space to create JavaScript strings, and then convert the original java and script strings.

Convert to string:
use toString method, but null and undefined do not.
Any type can be converted using the String function, following the following rules: if the
value has the toString() method, call this method; if the
value is null, return "null"; if the
value is undefined, return "undefined";

8Object

Definition: A set of data and functions. Any attributes and methods of the Object type also exist in more specific objects.
It has the following properties and methods:

  • constructor: holds the function used to create the current object
  • hasOwnProperty(propertyName): Used to check whether the given property exists in the current object instance.
  • isPrototyoeOf(object): Used to check whether the passed in object is the prototype of the current object
  • propertyIsEnumrable(propertyName): Used to check whether a given property can be enumerated using for-in.
  • toString(): returns the string form of the object
  • valueof(): Returns the string, numeric or boolean representation of the object.

Guess you like

Origin blog.csdn.net/weixin_42898315/article/details/110770034