Home  >  Article  >  Web Front-end  >  JavaScript is really not difficult - review the basics_Basics

JavaScript is really not difficult - review the basics_Basics

WBOY
WBOYOriginal
2016-05-16 17:43:36881browse
Foreword
Before talking about Jquery, let’s first learn the basic knowledge issues in Javascript (hereinafter referred to as JS) language. At that time, the basic knowledge was similar for each programming language. For variables, , functions, conditional statement blocks, loop statement blocks, etc. The writing method is different for each language. For example, when defining variables in JS, you have to use var to declare the local variable, and for weak For the type language JS, you can also not add var, but if you don't add it, the variable will be considered a global variable. This should be noted.

Variable
The value can change during the running of the program (haha, the definition in the book more than 10 years ago)
var people; //Declare a name Variable for people
var people="good boy"; //Assign a value to the variable when declaring it. The browser automatically interprets it as a character variable
var age=23; //Declare a local variable, type For integer
age=30; //Declare a global variable of type integer. During program execution, it will not be released

Function
In order to implement a Function organizes some code blocks together to form a whole. We call it function. It can greatly reduce the amount of code duplication and make the program clearer
Copy the code The code is as follows:

// Standard writing
funciton helloFun(){
alert("hellow world")
}
//How to write variable form
var helloFun=function(){
alert("hellow world")
}
// Function can have parameters, it is a weak type
var helloFun=function(msg){
alert(msg);
}
// Function call
helloFun("hello world");

Conditional statement
For one thing, there are multiple results. At this time, conditional statement appears. If the condition fixes several values, you can use switch, otherwise use if...else, see the code
Copy code The code is as follows:

// switch instance
var inputNumber=document.getElementByID( "type");
switch(inputNumber)
{
case 1:
alert("Type 1");
break;
case 2:
alert( "Type No. 2");
break;
case 3:
alert("Type No. 3");
break;
default:
alert("throw new Exception( )");
break;
}
// if instance
var inputAge=document.getElementByID("age");
if(inputAge>1 && inputAge<18)
{
alert("Underage");
}
else if (inputAge>=18 && inputAge<70)
{
alert("Adult");
}
else if(inputAge>=70)
{
alert("elderly")
}
else
{
alert("There is an error in filling out the form") ;
}

Loop statement
means to repeatedly execute a code block when a certain condition is followed. We can use while, for, etc.
Copy code The code is as follows:

// for loop
var arr=[1 ,2,3]
for(int i=0;j=Arr.length;iconsole.log(arr[i]); // The Firefox console can see Result
}

In fact, in the JS world, the execution performance of the code is also very particular. The performance of the way we write the for statement is not wrong, but if it is written as follows, the performance There will be a decrease, because every time it traverses
, it will find the length of Arr.
Copy code The code is as follows:

// for loop
var arr=[1 ,2,3]
for(int i=0;iconsole.log(arr[i]); // Poor performance
}

Okay, that’s it for writing the basic knowledge of JS programming. From next time, I will introduce the relevant knowledge of jquery. Thank you for reading!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn