js foundation 2

1.in:

For array properties, you need to specify the index value in the form of a number to represent the property name of the array

var arr = ["a","b","2","3","str"];
var result = ("b" in arr);
var result1 = (4 in arr);
console.log(result+'\n');
console.log(result1);

The output is:

speak

ture

For general object properties, you need to specify the name of the property as a string

Such as:

var goods={name:'instant noodles',count:1,year:1998}

if('name' in goods) // true

if('count' in goode)// true

2: indexOf() usage

 You can determine whether a string appears in an array;

 

Such as:

 

var year=[1993,1994,1995,1996]

 

if(year.indexOf(1993)==-1)//return flase. If 1993 is not in the year array, return true.

 

if(year.ondexOf(2001)==-1)//return true. If 2001 is in the year array, return flase.

 

This method will retrieve the string from beginning to end to see if it contains a substring, and if so, return the position of the first occurrence of the substring

 

Such as:

 

var string = 'hello word'

console.log(string.indexOf('e')+'\n');

console.log(string.indexOf('w')+'\n');

console.log(string.indexOf('W'));

result:

1

6

-1

3.js ternary operator

 The syntax is  conditional ? result 1: result 2  ; . Here you write the condition in front of a question mark (?) followed by result 1 and result 2 separated by a colon (:). The result is 1 if the condition is met, otherwise the result is 2.

Such as

var a=1;

(a==1) ? a='true':b='flase';

console.log(a)

result: true

 

4: The role of the double vertical bar in the return value of the js function

 

 

|| means or,

var a=b||c;

It means that if b has a value, a=b, if b has no value, a=c. If there is no value, it is undefined .

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326946231&siteId=291194637