Sharing: JavaScript common face Test __java

Source: Internet
Author: User
Tags function definition numeric value object object
JavaScript

1.1 Briefly describes the data type of JavaScript.

The data types of JavaScript can be divided into primitive types and object types.

The original type consists of string, number, and Boolean three. Where strings are used with either single or double quotes, and numeric types are stored in 64-bit floating-point format, without distinguishing between integers and decimals, and Boolean (logical) types can have only two values: TRUE or FALSE.

Complex types refer to other objects, such as array, Date, object, and so on.

In addition, JavaScript has two special original values: null (empty) and undefined (undefined), which represent unique members of their particular type.

1.2 Read the code, write the result

Write out the results of the following expression: The results of the preceding code are:

var a=[];

var b=a;

B[0]=1;

Console.log (A[0]); 1

Console.log (B[0]); 1

Console.log (A===B); True

Assign an object to a variable, just a reference to a number assignment, and the object itself is not assigned once, so the variable A and variable B point to the same array.

1.3 Brief description of the difference between null and undefined

Null: is a JavaScript keyword that describes the null value, performs a typeof operation on it, returns "Object", which is a special object value that can represent "no value" for numbers, strings, and objects.

Undefined: is a predefined global variable with a value of undefined, which is a value of a variable that indicates that the variable was not initialized. When querying an object property, the value of an array element, returning undefined indicates that the property or element does not exist, and returns undefined if the function does not have any return value.

It should be noted that although null and undefined are different, they are interchangeable because they all represent "value vacancies." Therefore, using the "= =" task is equal, you need to use "= =" to differentiate them.

1.4 Read the code, write the result

Write out the results of the following expression: The results of the preceding code are:

10+ "Object" 10object//Convert to String

"7" * "4" 28//Convert to character

1-"X" Nan//cannot be converted to a numeric value for calculation, so return Nan

1 + {}1[object Object]//Return object's ToString () result is added by string

True + true2//bool type converted to number type

2 + null2//null conversion to value 0

1.5 Read the code, write the result

Write out the results of the following expression: The results of the preceding code are:

var a=2;

var obj={x:1,y:{z:2}};

Var n=[obj,3,[4,5]];

Console.log (A<<2); 8

Console.log (obj["y"].z); 2

Console.log (n[0].y["z"]); 2

Console.log (n[2]["1"]); 5

Delete N[0];
Console.log (n[0].x); This line of code errors output error

1.6 Read the following code:

var x=10;

var y=20;

var z=x<y?x++:++y;

After the above code is run, what is the value of the variable x, y, and z?

Answer: After the above code is run, the value of the variable y is 11; the value of the Var is 20; The value of the variable z is 10.

This is because, when you run the third line of code, you execute only. The first statement after, therefore, the value of Y is not changed, still 20, and the value of x is assigned to the variable z, so the value of Z is 10, and then the value of x is increased by 1, to 11.

1.7 What is "logic short circuit".

Logic Short circuit in terms of logical operations, refers to the calculation of only a part of the logical expression can be a disadvantage of the result, but not the whole expression to calculate the phenomenon.

For the && operator, the second operand is not judged when the first operand is false, because at this point the result of the final operation must be false, regardless of the second operand;

For "| | operator, the second operand is not judged when the first operand is true, because at this point the final result of the operation must be true regardless of the second operand.

1.8 Read the following code

var empage=20;

var EmpName;

if (empage>20&&empname.length>0) {

Console.log (1);

}else{

Console.log (2);

}

If the above code runs, it will produce a valid output or a code error.

Reference answer: The above code runs, will output 2, without error.

This is because if the first condition of the logical expression in the IF condition (empage>20) is dissatisfied enough, it returns false, and a logical short-circuit will occur without continuing to judge the next condition. Therefore, even if the variable empname in the next condition does not have an assignment, the error will occur if the calculation empname.length occurs, but the expression is not evaluated because of a logical short-circuit, so no error occurs.

If the logical expression in the IF condition returns false, the Else statement is run: Output 2.

1.9 Explain the difference between local and global variables in JavaScript

Global variables have global scope and can be accessed anywhere in the JavaScript code; variables declared within a function are defined in the function body, which is local, and its scope is localized.

It is important to note that if you declare a local variable in a function body, if you do not use the var keyword, the global variable is declared.

1.10 Read the code, write the result

Write out the output of the following code:

var x= "global";

function Test () {

var x= "local";

return x;

}

Console.log (Test ());

Reference answer: The output of the above code is local. This is because, in the function test () body, local variables have higher precedence than global variables of the same name. Therefore, if local variables and global variables are declared in the function body with the same name, the local variable takes precedence. Therefore, when the function test () is invoked, the local variable x is returned, and its value is locally.

1.11 What is a function scope in JavaScript

The function scope in JavaScript is that a variable is defined in the body of the function that declares it and in any function that is nested within the function body. This means that all variables declared in the body of a function are always visible throughout the body of the function, which is also known as "declaration advance", where all variables declared within a function (not related to assignment) are advanced to the top declaration of the function.

For example, look at the following code:

function Test () {

Console.log (x);

var x=10;

Console.log (x);

}

Test ();

The appeal code runs, will first output undefined, and then output 10.

This is because, although the variable x declares and assigns a value in the second row, Dan its valid range is the entire function body, therefore, the first line of code output, indicating that the variable x has been declared but not assigned, so the output undefined; When the third line of code runs, the output is 10 because the variable x has already been assigned a value of 10.

1.12 Read the code, write the result

Write out the output of the following code

function Test () {

var sum=0;

for (Var i=0;i<10;i++) {

Sum+=i;

}

Console.log (sum);

Console.log (i);

}

Test ();

In the code above, the value of the output sum is 45; the value of output I is 10.

This is because, in the function test () body, after the loop is calculated, the value of the variable sum from 0 to 10 is 45; the variable i is declared in the for loop, but is valid throughout the function (the function is left and right), so the value of the variable i is 10 after the loop completes.

1.13 Read the code, write the result

Write out the output of the following code:

var x= "global";

function Test () {

Console.log (x);

var x= "local";

Console.log (x);

}

Test ();

In the above code, first output undefined, then output local.

When the function test () body declares a local variable x with the same name as a global variable, the global variable is overwritten, that is, the local variable takes precedence. Therefore, the first time the variable x is output, the output local variable x, when the variable x is declared and not assigned, so the output undefined; the second time output variable x, the local variable x is already assigned, so the output string is localized.

1.14 briefly on the role of Arguments objects

In function code, you can access the parameters of a function by using a special object arguments. That is, when a developer defines a function, you do not need to explicitly declare the parameter for the method, or you can use arguments in the method body to access the parameter. This is because arguments is a special object that represents an array of arguments for a function in the function code.

Because arguments represents an array of parameters, you can first use Arguments.length to detect the number of parameters of a function, and second, you can access a parameter by subscript (Arguments[index]). This can use the arguments object to judge the number of parameters passed to the function and get the parameters, which is suitable for the number of function parameters can not be determined.

1.15 Briefly describe several ways to define functions in JavaScript

In JavaScript, there are three ways to define a function:

Function statement: That is, use the function keyword to display the definition function. Such as:

function f (x) {return x+1;}

function definition expression: Also known as "function direct quantity". Such as:

Var f=function (x) {return x+1}

Use the function () constructor definition. Such as:

var f=new Function ("x", "Return x+1;");

1.16 Read the code, write the result

Write out the output of the following code:

var f=function (x) {return x*x}

Console.log (f);

Console.log (f (10));

When the above code runs, the function (x) {return x*x} is exported first. ; then output 100.

This is because the variable F represents a function object, so when you output the variable directly, the character literals F (10) corresponding to the output function body is called to call the function corresponding to the variable F, which returns 100 and outputs.

1.17 Read the following code

function f () {

Console.log ("function");

}

function Test () {

Console.log (f);

f ();

f= "Hello";

Console,log (f);

f ();

}

Test ();

The above code is run and will be first output: function f () {Console.log ("function");} ; then output the function, and then output hello; then output exception information: string is not a function.

This is because, when a function is defined, the function name is used as a global variable, which points to the function object. Therefore, when you execute the first line of code in the function test, you output the function object corresponding to the variable F, which is the string form of the code in the output function body; then execute F

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.