Learn about JavaScript basics

Source: Internet
Author: User
Tags bitwise bitwise operators cdata script tag

As a front-end worker, you should be aware of some JavaScript development history, JavaScript implementations, and versions. The basic concepts include syntax, keywords, variables, data types, operators, statement controls, and functions, and they are basically similar to the other languages we learn, C + +, Java, C #, and so on, but there are subtle differences. Specific differences and basic learning content refer to JavaScript Advanced programming related chapters. first, the basic content should pay attention to several places1) Our most common wording for the script tag is as follows: <script type= "Text/javascript" > function () {console.log ("Hello world!"); }</script> So what are these usages? <script type= "Text/javascript" >  //<!    [cdata[function () {console.log ("Hello world!"); } //]]></script> in XHTML, like some special symbols,<> and so on will default to the label, the label can not be followed by a space. So if you want to achieve the same result under XHTML, you should escape &lt, but nobody wants to see it. Then you can wrap it up in CDATA. But there is a special browser does not support CDATA What to do, then add//<script type= "Text/javascript" > <!--function () {console.log ("Hello world!"    ); } //--></script> The above code is to prevent some browsers do not support JavaScript (this worry is now basically dismissed, there is a special label <noscript><p> Support and open JS Browser can see me </p></noscript>), in the form of HTML comments wrapped up, support JS Browser will execute. Last//Prevent JS execution to---think of syntax error <script type= "Text/javascript"defer= "Defer"> function () {console.log ("Hello world!"); }</script> is generally used for external browsers, defer indicates that the execution of the script is deferred until the entire page resolution is complete before running, with 8 summaries of the immediate download deferred execution <script type= "Text/javascript"Async>    function () {        console.log ("Hello world!");    }</script> is generally used for external browsers, as the name implies, do not let the page wait for the download of the script, the rest of the page can be loaded.          It is also recommended that the CSS code be put into the head, while the JS code is placed at the bottom of the body. Because HTML loading is from top to bottom, if there are a lot of scripts that need to be loaded before the body loads, then our users will see the page blank for a long time. Instead, when the page is displayed in front of the user, the user doesn't care what else is not loaded. This gives the user an illusion that our pages are "faster" than others. Then someone said to hurry, why not put the CSS file below? If this is the case, if the page is larger, the style is more likely to start showing to the user will be ugly.          It is best to write less </script&gt in the JS code, but not to write <\/script&gt, because when </script&gt appears in the character; Then the HTML will be judged as the end of <script>, the code behind will not be executed.   In the process of loading JS (especially JS in the head, this will block the loading of the UI), it is not recommended to dynamically modify the DOM elements, which we can imagine. &NBSP;2) loose variables and typeof loose variables mean that variables are "random" and can hold arbitrary types. However, we do not recommend such use when used as: Var number=10;       namber= "Ten", so do not error, but it is better not to use it. Because of the randomness of the variable, it is best to determine the current type of the variable by means of TypeOf, which solves this problem well Console.log (typeof "Hi Jack");//stringconsole.log (typeof 10);// Numberconsole.log (typeof null);//objectconsole.log (typeof message);//undefinedvar message= "Hello Jack Li"; Console.log (typeof message);//hello Jack Li3) Special cases in the data type  undefiedis a value of type undefied, it has only this one value. When declaring a value but not initialized it will be reported undefined as follows:
var message;//actually this is the equivalent var message=undefined;
Console.log (message);//undefined if so? var error;console.log (error);//This will be an error and cannot be executed What's even more outrageous is if this is the case? Console.log (typeof error);//undefined is he dizzy? In short typeof can avoid a lot of unnecessary mistakes. It is not recommended to use both of the above. It is a good habit to judge by TypeOf before using variables, personal understanding.NullThe type is also only a value of NULL, which is a pointer to an empty object, so typeof null is the same as object, and it is also recommended that the non-null undefined be derived from null before using the same procedure. So Console.log (null==undefined);//true   the Boolean type is also a very special typeIt has only two literal values, true and false, which are case-sensitive, as is the case in other languages, and judging a variable, especially a value in a radio or drop-down box selected by the front-end user, must be toLowerCase (), Unify to lowercase or uppercase and then compare.
Any type of variable can be converted to a Boolean value. Using the method Boolean (), the conversion table is as follows.
Type The value converted to true Value converted to False
Boolean True False
String Any non-empty string "" Empty string ' is also false note no spaces
Number Any number that is not 0 0 NaN
Object Any object Null
Undefined N/A Undefined
numeric types and Nan integer floating-point numbers need to be aware of floating-point numbers such asif (0.1+0.2==0.3)//is always falseFor a particular floating-point calculation, it is better not to compare the results, floating point is the precision of 17NaN full name is not a number. It and any number of operation results are Nan.number/0The result is Nan. There is also a function that determines whether NaN is IsNaN () IsNaN (//falseisnan) (Nan)//trueisnan ("")//falseisnan (True)//falseisnan ("message")// True numeric conversions for some special results, how much do you know?

Number ("Hello Jack Li");//nan

Number (true);//1

Number ("000123"),//123 generally more commonly used is parseint () parsefloat () parseint ("123ignore")//123parseint ("");//nanparseint ("0xA") //10 to eliminate parseint ("070")//This conversion, we'd betterSpecify a second parameter, which clearly indicates how many conversions parseint ("070", 8);//Parsefloat ("123.123.123");//123.123String Type single quotation marks have the same meaning, but cannot be nested in a cross-nest usevar message=null;var error;console.log (String (message)),//nullconsole.log (String (Error)),//undefined object type, This type is more complex and requires a separate 3) operator arithmetic operator,Bitwise Operators, relational operators,Equality operators ~ Bitwise non-
& Bitwise AND
| bitwise OR
>> Right Move front reserved sign bit
<< left Shift
>>> unsigned right shift front complement 0 Special cases of equality
null==undefined//truenan== "NaN"//false123==nan//false
Nan==nan//falseFalse==0//trueundefined==0//false1 23== "123"//trueCongruent = = =!==compared to the above, we can see that = = = requirements are higher, the value of the conversion is not equal. 123=== "123"//falseNull===undefined//false 4) Statement controlYou need to be aware of the for-in syntax inside, and the basic program flow control statements are similar. 

Learn about JavaScript basics

Related Article

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.