JavaScript interview slot Hui: data type

Source: Internet
Author: User
Tags character set numeric wrapper

  The basics of how we often get capsized in the gutter

Before summing up are some of the knowledge, will be, will not be able to answer, but in the JavaScript written writing process often encountered some of their own think it is very simple, but the answer is inexplicable problems, but also some of the questions will be stunned the problem, in the written process is almost all the mistakes of this kind of problem. You must have seen this topic     var f = function g () {return 23;}; typeof g ();//g is not defined or such topic   var foo = 1; function bar () {    foo =;     return;     function foo () {}} bar (); Console.log (foo);//1 Many similar topics, people have a headache, the answer is not the same as their own affirmation (follow-up blog will have a correlation), after eating many times after the loss also try to find some JavaScript quiz to do, every time when the abuse of the pieces, After the finish will feel that they can cope with the written test, but in the next written test and died in a similar topic, the bitter experience of the often error of JavaScript knowledge points after combing, correct rate really improved a lot, found in fact are some of the basic knowledge of JavaScript grammar, If we are in the written exam always in JavaScript trench hui fall instead of knowledge, then it is necessary to understand these humble basic knowledge, so serious introduction of the Interview experience blog I hope to start with the basic knowledge. The main content is read the "JavaScript Authority Guide" after understanding, really recommend to find a job students endure patience to read this good book.   Let's start with JavaScript type the JavaScript data type is simple, with the original type (primitive type, which is the value type of people word of mouth) and object type (so-called reference type) two types of original type   number, String, Boolean, null, undefined   object type   objects, function, Date, array, etc.   maybe you think it's simple, but if you don't have a system, the simple data type is written, The interview is also very dangerous, first understand the characteristics of these types   string   in many programming languages, string types are reference types, and a string in JavaScript is a baseThis type, in JavaScript, is a set of immutable ordered sequences of 16-bit values, each of which typically comes from the Unicode character set (the JavaScript Authority Guide). All underlying types in JavaScript can not be modified, this sentence for the type of number and other good understanding, 1 is not 2, but string "abc" Can not become "ABCD"? In fact, all we think is that the method of modifying a string simply returns a new string.   number   differs from most programming languages in that the numbers type of JavaScript does not differentiate between integers and floats, and all digits are valid for floating-point representations.   BOOL   This is relatively simple, the type is true and false two values   NULL   NULL is no stranger to programmers, indicating undefined (declared) variables   undefined   NULL It means "empty", what is undefined? We know that in a strongly typed language like Java, if we declare a variable without initializing it, the system assigns a default value of that type based on the variable type, but in JavaScript we declare the variable to be in the VAR keyword (or even not write), and the type of the variable is determined by its value. The variable is the array type, and the variable is the number, but what if we don't initialize it? You can't say the variable is null, after all, this variable has been declared, at which point we think of the variable as a undefined type variable and assign it a undefined type unique value--undefined (not "undefined")   wrapper object We often see some of these code     var s= ' test '; Console.log (S.indexof (e)); 1 Console.log (s.length); 4 at first glance, it's no surprise, but not a good one. String is a basic type? Basic types of variables where the methods and properties come from! Leave this knowledge for a while, anyway can so use is, see a question     var s= ' test '; s.len=4; Console.log (S.len);//undefined Pit Dad! Why not 4 and become a undefined? As long as the string attribute is referenced, JavaScript creates a temporary object by calling new string (s), the IndexOf method we use and LeThe Ngth attribute is derived from this temporary object, created once for each use, and then destroyed. This temporary object is the wrapper object, not just the String,number and the Boolean type. Know this knowledge to see the above topic why the result is undefined   The above code can parse     var s= ' test '; Create a String type variable s.len=4;//Create a wrapper object, add attributes to a wrapper object Len/ /References complete, destroy the wrapper object Console.log (S.len);//Create wrapper object, find its Len property, not found, return to undefined that's clear. Type conversion in the written question often encounter some problems to solve the problem of the last died in the gutter, Type conversions often act as sewers, look at examples     var a=[0], B=a, b=[1]; Console.log (A+B); We fixed the problem of the array, the final problem can be changed to [0]+[1], accidentally will be written as 1, but this time we call the array of ToString method, is actually ' 0 ' + ' 1 ' = ' 01 ', this time you can see the importance of type conversion knowledge. JavaScript's value type is very flexible and will automatically convert the type as needed, such as ' 1+ ' 234 ' JavaScript automatically converts 1 to the desired string ' 1 ', which is very obvious when we look at some of the default conversions   values that may be mistaken Convert to string convert to numeric convert to Boolean undefined "undefined" NaN false null "NULL" 0 false true "true" 1 false "false" 0 "" 0 false "2.5" 2.5 True "string" NaN true 0 "0" false 1 "1" True Nan "Nan" false Infinity "Infinity" true [2] "2" 2 true explicit type conversions (calling constructors or par Seint () functions, etc.) we can easily find, but many of the JavaScript operators can throw implicit type conversions     Number ("3");//3 String (false);//false Boolean ([]); True Object (3);//new number (3) 1+ "234";//"1234"5+ "";//string (5) + "5";//5, changed to number ("5") "5" -0;//5, also digital ToString and valueof In some cases we will convert the object to a basic type, which is simple for converting to a Boolean rule. All objects will be converted to true, but our most common use is to convert objects to strings or numbers, which is a bit of a hassle. The process of converting an object to a string object is probably 1. This method is called if the object has a ToString method, and if the method returns a base type (the one with the pit dad does not return a string type), JavaScript converts the value to a string that returns 2. If the object does not have a tostring method, or if the method returns a primitive type, the valueof method is invoked, and the value is converted to string output 3 if there is a valueof method and valueof returns the base type. And if you can only get an error.   Convert a digital object into a digital one the process is probably 1. If the object has a valueof method, and the method returns the base type, converts the value to a number to return 2. If the object has a ToString method, and the method returns the base type, the conversion to the number returns 3. Otherwise, the error   see a topic     var o = {        X:8,         Valueof:function ( {            return this.x + 2;        },         Tostring:function () {            return this.x        }   &NBSP ; }; Console.log (o+ ' 1 ');//"Console.log" (o+1);//11 Note that one of the returned is a string, a number, the basic knowledge of the above, you are not correct   judge data type We can use the TypeOf operator to determine the data type     TYpeof undefined; "undefined" typeof null; "Object" typeof true; "Boolean" typeof 0; "Number" typeof NaN; "Number" typeof "string"; ' String ' typeof function () {}; "function" typeof []; "Object" typeof new Date (); "Object" There are a few look strange need us to note that TypeOf is an operator, not a method, that is, and add a minus sign, regardless of priority, do not need to add parentheses to the operand, of course, add also no matter, to tell the truth readability is very high   typeof return values are lowercase strings   NULL types are NOT NULL, but "object"   Nan This is not a numeric type and "number"   function is also an object, but TypeOf gave " Function "type   other objects return" object ", very unrecognized   specific object type using the instanceof operator, this is a little bit of memory, for the basic type, instanceof always return False     1 instanceof number; False new Number (1) instanceof number; True

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.