Six data types of JavaScript

JavaScript data types

    Basic data types: Undefined, Null, Boolean, Number, String.

    Complex data type: Object.

 

   1.Undefined

    Only one value of this type is undefined, a variable declared with var but not assigned a value

 

var message;
alert(message == undefined) // true
typeof message // "undefined"

 

 

    2.Null 

    Only one value of this type is null, indicating an empty object pointer.

var message = null;
typeof message; // "null"

 

    3.Boolean

    Values ​​of this type have both true and false which are case sensitive

 

// value converted to true
var value = true;
var value2 = "hello world"; // any non-empty string
var value3 = 1; // any non-zero number
var value4 = {}; // any object including function, Object

if (value & value2 & value3) {
    alert("value is true");
}

// value converted to false
var value = false;
var value2 = ""; // empty string
var value3 = 0; // 0 or NaN
var value4 = null; // null
var value5 = undefined; // undefined

if (value & value2 & value3 & value4) {
    // won't come in
} else {
    alert("value is false");
}

typeof  true; // "boolean"

   

 

    4.Number

 

var octalNum1 = 070; // octal 56
var octalNum2 = 079; // invalid octal - 79
var octalNum3 = 08; // invalid octal - 8
// The above assignment is invalid in strict mode and the browser throws an exception

var hexNum1 = 0xA; // 10 hex
var hexNum2 = 0x1f; // 31 hex

    4.1 Floating point values

 

    Must contain one digit after the decimal point

 

var floatNum1 = 1.1 or var floatNum2 = 0.1

    Use e notation to represent values

var floatNum = 3.125e7; // 31250000

    The highest precision of floating point numbers is 17 decimal places

// floating point budget loses precision
var a = 0.2;
var b = 0.3;
a + b; // 0.50000000000000004

    4.2 Value range

 

    The minimum value is stored in Number.MIN_VALUE with a value of 5e-324, and the maximum value is stored in Number.MAX_VALUE with a value of 1.7976931348623157e+308.

     If the calculated value is out of range, the value will be automatically converted to an Infinity value. You can use the isFinite() function to detect whether the value range is exceeded

var num = Number.MAX_VALUE + Number.MAX_VALUE; // Infinity
isFinite(num);  // false

    4.3 NaN

 

    NaN, which means Not a Numer is a special value that returns NaN when the calculation results in an error.

    The global function isNaN(), accepts any parameter to determine that the parameter is "not a number".

    Two properties: any operation involving NaN returns NaN, and NaN is not equal to any value.

NaN / 10; // NaN
NaN == NaN; // false

isNaN (NaN); // true
isNaN(10); // false (is a number)
isNaN("10"); // false (can be converted to a number)
isNaN("name"); // true (cannot be converted to a number)
isNaN(true); // false (can be converted to value 1)

    4.4 Numeric conversion

 

    There are 3 functions to convert non-numbers to numbers, Number(), parseInt(), parseFloat().

 

// Number()
var num1 = Number("hello world"); // NaN
var num2 = Number(""); // 0
var num3 = Number("00011"); // 11
var num4 = Number(true); // 1
var num5 = Number(false); // 0

// parseInt()
var num1 = parseInt("1234hello world"); // 1234
var num2 = parseInt(""); // NaN
var num3 = parseInt("0xA"); // 10(hex)
var num4 = parseInt (22.5); // 22
var num5 = parseInt("70"); // 70
var num6 = parseInt (true); // NaN

var num1 = parseInt("10", 2); // 2 (parsed in binary)
var num2 = parseInt("10", 8); // 8 (parsed in octal)
var num3 = parseInt("10", 10); // 10 (parsed in decimal)
var num4 = parseInt("10", 16); // 16 (parsed in hex)

// parseFloat()
var num1 = parseFloat("1234hello world");  // 1234
var num2 = parseFloat("0xA"); // 0
var num3 = parseFloat("22.5"); // 22.5
var num4 = parseFloat(22.34.5); // 22.34
var num5 = parseFloat("0908.5"); // 908.5  
var num6 = parseFloat("3.125e7"); // 31250000

    5. String

 

    The String type is used to represent a sequence of characters consisting of zero or more 16-bit Unicode codes, that is, a string.

   

    5.1 Character literals

  •     \n // newline
  •     \t // tab
  •     \b //backspace
  •     \r //Enter
  •     \\ //feed
  •     \' //apostrophe(')
  •     \" //Double quotes(")
  •     \xnn // a character in hexadecimal code
  •     \unnn //A Unicode character in hex code

    5.2 Characteristics of Strings

 

var lang = "java";

lang = lang + "Script"; // "javaScript"
long = long + 1.8; // "java1.8"
lang = lang + null; // "javanull"
lang = lang + undefined; // "javaundefined"
lang = lang + true; // "javatrue"

    5.3 Convert to String

 

    

var str  = "hello world";
var num = 10;
var bool = true;
var func = function () {};
var obj1 = {};
var obj2 = new Object();
var arr1 = [];
var arr2 = ["a","b"];

str.toString();     // "hello world"
num.toString();     // "10"
bool.toString();    // "true"
func.toString();    // "function(){}"
obj1.toString();    // "[object Object]"
obj2.toString();    // "[object Object]"
arr1.toString();    // ""
arr2.toString();    // "a,c"

var value1 = 10;
var value2 = true;
var value3 = null;
var value4 =;

String(value1); // "10"
String(value2); // "true"
String(value3); // "null"
String(value4); // "undefined"

    6.Object

 

    Create an instance of the Object type

// literal method
var o1 = {};
// new operator creation
var o2 = new Object();

    Each Object instance object has the following properties and methods:

  •     constructor: Holds the function used to create the object - Object().
  •     hasOwnProperty(propertyName): Check whether the given property exists in the instance of the current object (not in the prototype)
    var O = {
        "name": "zhang san",
        "age": 25
    };
    O.hasOwnProperty("name");  // true 
  •     isPrototypeOf(object): Used to check whether an object exists on the prototype chain of another object.
    // define function object
    function Foo() {}
    function Baz() {}
    // inherit the prototype of Foo
    Baz.prototype = Object.create(Foo.prototype);
    // create a new object
    var baz = new Baz ();
    // Check if baz inherits from Foo
    Foo.prototype.isPrototypeOf(baz); // true 
  • propertyIsEnumerable(propertyName): Used to check whether a given property can be enumerated with a for-in statement.
    var o = {};
    var a = [];
    o.prop = 'is enumerable';
    a[0] = 'is enumerable';
    
    o.propertyIsEnumerable('prop');   // true
    a.propertyIsEnumerable(0);    // true
    
    a = ['is enumerable'];
    a.propertyIsEnumerable(0); // returns true
    
    // Properties of built-in objects cannot be enumerated
    Math.propertyIsEnumerable('random');   // 返回 false
    
    /************************Own properties and inherited properties******************** ******/
    // own properties cannot be enumerated
    var a = ['is enumerable'];
    a.propertyIsEnumerable('constructor') // returns false
    a.propertyIsEnumerable('length');   // 返回 false
    
    
    function firstConstructor() {
      this.property = 'is not enumerable';
    }
    
    firstConstructor.prototype.firstMethod = function() {};
    
    function secondConstructor() {
      this.method = function method() { return 'is enumerable'; };
    }
    
    secondConstructor.prototype = new firstConstructor;
    secondConstructor.prototype.constructor = secondConstructor;
    
    var o = new secondConstructor();
    o.arbitraryProperty = 'is enumerable';
    
    o.propertyIsEnumerable('arbitraryProperty');   // 返回 true
    o.propertyIsEnumerable('method'); // returns true
    o.propertyIsEnumerable('property'); // returns false
    
    o.property = 'is enumerable';
    
    o.propertyIsEnumerable('property'); // returns true
    
    // These return fasle because, on the prototype chain, propertyIsEnumerable is not considered
    // (although the last two can be looped out in the for-in loop).
    o.propertyIsEnumerable('prototype'); // returns false (according to JS 1.8.1/FF3.6)
    o.propertyIsEnumerable('constructor'); // returns false
    o.propertyIsEnumerable('firstMethod'); // 返回 false 
  • toLocaleString(): Returns the result of calling toString().
  • toString(): Returns the string representation of the object
  • valueOf(): Returns the string, numeric or boolean representation of the object, usually the same as the return value of toString().
      

-- over

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326017272&siteId=291194637