Lead us to learn JavaScript basics (i) Basic concepts _javascript Skills

Source: Internet
Author: User
Tags bitwise bitwise operators

Learning Catalog

One, data type

Data type

Second, operator

Increment and decrement operators:

Bitwise operators:
Boolean operator:
Additive operator:
Equality operator

Third, the statement
For-in statement
Label statement

One, data type

Data type

Basic data types (five kinds)

Undefined
Null
Boolean
Number
String

Complex data types (one)

Object
Undefined: Only one value Undefined (no assignment defined).

Cases:

var str;alert (str);//undefined

Note: If you do not use Var, you will report an exception (... is not defined) directly.

Recommendation: Each var declaration is assigned an initial value, you can reduce a lot of unexpected situations.

Such as:

var str;var str2= "John"; str = str + str2;alert (str); it's not the "John" we expected.

Null: Slightly

Boolean: Only two literals: true and false.

Any data type can be converted to a Boolean type through a Boolean () function.

String: Non-null character =>true null character =>false
Number: Any non-0 =>true 0 and Nan=>false
Object: Any objects =>true Null=>false
Undefined Undefined=>false

Such as:

var str = boolean (NaN); var str2 = Boolean ("John"); var str3 = Boolean (undefined); alert (str+ "+str2+" "+STR3);

That being the case, we automatically execute the Boolean conversion when we use the IF statement.

if (str) {
  //str as long as non-null characters, any non-0 digits, any non-null objects are true
  //str as long as the null characters, 0, NaN, null, undefined are all false
  / We have each judged
  alert ("true");

String: Slightly

Second, operator

Increment and decrement operators:

"++num 、--num, num++, num--"

var age = 3, Age2 = 3;age++;++age2;alert (age+ "" +age2);

are 4, as if they can't see any difference.

var age = 3, Age2 = 3,num1, num2;num1 = age++ + 1;num2 = ++age2 + 1;alert (num1+ "" +num2);

OK, here's the difference. One is still 4, one becomes 5. Why is that? Priority is different, age++ first executes the operator + and then adds the 1,++age to itself by adding 1 and then the + operator plus 1.

Bitwise operators:

"Bitwise non-not (~), Bitwise AND and (&), bitwise OR OR (|), bitwise exclusive OR XOR (^), left (<<), signed right Shift (>>), unsigned Right shift (>>>)"
In fact, we rarely use, here a little understanding.

Example: Bitwise NON

var num1 = 25; Binary 00000000000000000000000000011001
var num2 = ~num1; Binary 11111111111111111111111111100110
alert (num2); -26

Example: by-bit and

var result = & 3;
alert (result); 1
//*********************
= 0000 0000 0000 0000 0000 0000 0001 1001
3 = 0000 0000 0000 0000 0000 0000 000 0 0011
---------------------------------------------

Example: Bitwise XOR OR

var result = 3;
alert (result);
//*************
= 0000 0000 0000 0000 0000 0000 0001 1001
3 = 0000 0000 0000 0000 0000 0000 0000 001 1
---------------------------------------------
XOR = 0000 0000 0000 0000 0000 0000 0001 1010//Decimal Value 26

Example: Move left

var oldValue = 2; is equal
to binary var newvalue = OldValue << 5;//equals binary 1000000, decimal 64

Example: Sign Right move

var oldValue = 64; Equal to binary 1000000

Example: Unsigned right shift (this difference is a bit large, because the 32nd 1 represents a negative value also moved over)

var oldValue =-64; equal to binary 11111111111111111111111111000000
var newvalue = OldValue >>> 5; 00000111111111111111111111111110 is equal to 134217726 of the decimal

Boolean operator:

"Logical Non-(!), logical AND (&&), logical, or (| | )】

Here's the thing to say && and | | is short-circuit operation. | | If one of the previous conditions is met, the following condition is not detected,&& the opposite of the previous condition does not detect the following conditions.

Cases:

var age = 3;
if (age| | AAA) {//The age of this is a boolean ture, so the back of AAA is not detected.
alert (age);}
if (aaa| | Age) {//This will cause an error
alert (age);}

Test:

var age = 3;if (age| | AAA) {//The age of this is a boolean ture, so the back of AAA is not detected. alert (age);} if (aaa| | Age) {//This will cause an error alert (age);}

Here is a small trick to tell you that, many times we don't know if a variable has a value, but we need to use it properly, so we can give a spare tire.

such as: Browser compatibility issues, in the old browser is to use Window.event to take the event of the relevant attribute values, the new browser is directly through the form of formal parameters passed over. We can just.

function Myonclick (EV) {
  var myevent = EV | | | window.event;//this will ensure compatibility of the new and old browsers
}

Experiment:

var obj = Null;var Obj2 = obj| | " Spare tire "; alert (OBJ2);

Additive operator:

"Addition (+), subtraction (-)"

It's a familiar one, but there's still a point to be aware of.

var strnum1 = "5";
var num2 = strnum1 + 3;
var num3 = strnum1-3;
Alert ("num2=" +num2+ "num3=" +num3);//What will be the result here?

Let's test it out.

var strnum1 = "5"; var num2 = strnum1 + 3;var num3 = Strnum1-3;alert ("num2=" +num2+ "num3=" +num3);//What will be the result here?

Why is that? Strings and numbers are added, and numbers are converted to strings. Strings and numbers are subtracted, and strings are converted to numbers.

Equality operator

var num1 = "5"; var num2 = 5;var isequal = Num1==num2;alert ("NUM1 is equal to num2:" + isequal);

Strangely, the string is equal to the number. Yes, the automatic transformation is used here. But what if we don't want to see such a result?

var num1 = "5"; var num2 = 5;var isequal = Num1===num2;alert ("NUM1 is equal to num2:" + isequal);

Yes, we can use = = = All equals. Not only compare values, but also compare types.

Third, the statement

For-in statement

For loop statements, for-in may be used less often. (The for-in loop should be used on the traversal of non-array objects, and looping using for-in is also known as "enumerations.") )

Cases:

var obj = {id:2,name: "john"};var str = "" for (o in obj) {str = "property:" + O + "value:" + obj[o] + "";} alert (str);

Label statement

It feels like a goto in C #, but it's not the same.

Do we usually have to jump out of the second loop inside multiple loops? Generally we can only use break or continue jump out of the innermost loop, or return out of the entire function. Unexpectedly, there is also a label statement, you can jump out of any layer loop.
Cases:

var num = 0;outermost:for (var i=0 i < i++) {for (Var j=0; J < + j) {if (i = = 5 && j = 5 ) {break outermost; } num++; }}alert (num); -

Well, the first article is almost finished here. Finally, a few interesting topics for everyone.

One, why 1=0.9999999999 ...
Ii. why alert (0.1 + 0.2)//0.30000000000000004
Iii. why alert (0123 + 4)//87

Four

var obj1 = {}; Obj1.name2 = "John";
var str1 = "Test"; Str1.name2 = "Dick";
Alert (obj1.name2 + "" + str1.name2);
What is the popup value?

Five

var obj1 = {name2: "111"};
var obj2 = obj1;
obj1.name2 = "222";
alert (obj2.name2);//What's popping up here?
Obj1 = {name2: "333"};
alert (obj2.name2);//What's popping up here?

Six

var fun = function (NUM1) {
if (NUM1 <= 1) {
return 1;
}
else {
Return NUM1 * Fun (NUM1-1);
}
}
var fun2 = fun;
Fun = function () {
return 1;
}
Alert (FUN2 (5));//What does this pop up?

There's a little time for you to introduce JavaScript basic data types

The basic data types for JavaScript include 6 kinds: number/string/boolean/object/function/undefined.

  2.1 Number Type

The number type is used to store numeric values, which describe 64-bit floating-point values. However, JavaScript does not represent all values between 0-2e64, because it also needs to represent non integers, including complex numbers, fractions, and so on. For 64-bit, you need to use 11 bits to store the decimal parts of a number, and 1 bits to indicate positive or negative, so JavaScript can actually represent a value between -2e52 and 2e52.

  2.2 String Type

String types are used to represent text, you can use single or double quotes to include text, and any symbol enclosed in quotation marks is considered string, but for special symbols, you may need to escape processing.

  2.3 Boolean type

A Boolean type consists of only two values: True and False. We can use various Boolean expressions in a program to get true or false to implement different business branching processes.

We can include multiple conditions in an expression, which can be related to or not, and in the calculation, the precedence is as follows: | Has the lowest priority, followed by &&, then the comparison operator, and finally the other operator (for example!). )。

As with many other languages, for &&, when the previous condition is false, the following condition is no longer counted, for | | , the following condition is no longer evaluated when the previous condition is true.

Look at the following example:

function Conditiontest () {
   var a =;
   var b =;
   var c = {"Key": "old"};
   Print (c["key"]);
   if (a==) print ("a =");
   if (a== && b==) print ("A = = && b = =");
   if (a== | | changevalue (c)) print (c["key"]);
   if (a== && changevalue (c)) print (c["key"]);
 function ChangeValue (obj) {
   obj["key"] = "changed";
   return true;
 }

Its output results are as follows:

Old
a = 1
a = = 1 && b = 1 Old
changed

Can be seen in the use of | | , the ChangeValue method is not invoked.

  2.4 Undefined type

When we declare a variable, but do not assign it a value, it is undefined, like the following

var b;
Print (b);

In JavaScript, there is also a value similar to undefined: null. Undefined indicates that the variable is declared but not replicated, and Null indicates that the variable is assigned but empty, and that the value of Undefined==null is true.

 2.5 Type Conversions

We mentioned above that the value of undefined = = NULL is true, but when we use the TypeOf operation we can see that null is the object type, which indicates that a type conversion occurred during the comparison.

A type conversion is a value that converts a value of one type to another. We use = = to compare, there will be type conversion, we can use = = = to prevent type conversion.

Look at the following example:

function Converttypetest () {
   var a =;
   var B = "";
   Print ("A:" + a);
   Print ("B:" + b);
   Print ("Type a:" + typeof a);
   Print ("type of B:" + typeof B);
   Print ("A==b:" + (A = = b));
   Print ("A===b:" + (A = = = b));
   Print ("A===number (b):" + (A = = number (b)));
   Print ("string (a) ===b:" + (String (a) = = b));
   Print ("Type of undefined:" + typeof undefined);
   Print ("Type of NULL:" + typeof null);
   Print ("Undefined==null:" + (undefined = null));
   Print ("Undefined===null:" + (undefined = = null));
 }

The output results are as follows:

A:1
b:1
type of a:number
type of b:string
a==b:true a===b:false a===number
(b): True
String (a) ===b:true
type of undefined:undefined
type of null:object
undefined==null:true
Undefined===null:false

You can obviously see the difference between = = and = =.

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.