Difference between revisions of "Operators"

From rbachwiki
Jump to navigation Jump to search
(Created page with "==Spread Operator == ''' Expands elements of an array ''' function addFourAges(a,b,c,d){ return a+b+c+d; }; var sum1 = addFourAges(18,19,20,21); ==Back To Top-[...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Spread Operator ==
==ES6 Spread Operator==
''' Expands elements of an array '''
'''ES6 Equivalent Spread Operator'''
  function addFourAges(a,b,c,d){
  const sum3 = addFourAges(...ages);
return a+b+c+d;
  // the ... means the ages array is expanded into individual components.
  };
var sum1 = addFourAges(18,19,20,21);


''' Join two Arrays '''
const arrayOne= ['hello', 'two'];
const arrayTwo = ['three', 'four'];
const arrayThree = [...arrayOne, ...arrayTow];
''' Looping through all elements and changing the color '''
const h = document.querySelector(h1)
const boxes = documwnt.querySelectorAll('.box');


const all = [h, ...boxes];
Array.form(all).forEach(cur=> cur.style.color='purple') // returns an array
== Rest Parameter ==
function isFullAge(...years){
years.forEach(cur=> (216-cur)>=18);
}
  isFullAge(1990,1921,1967);
== Default Parameters ===
function Person(first, last = 'bacchas'){
this.firstname = first;
this.lastname = last;
}
== Maps ==
''' New key value data structure'''
<pre>
const question = new Map();
question.set('question', 'what is');
question.set(1,'es5);
question.set(2, 'es6');
question.set(3, 'ES2015');
question.set('correct', 3);
question.set(True, 'Correct Answer');
question.set(false, 'wrong');
// Retrieve DAta
question.get('question')
// get size
question.size;
// delete
question.delete(2);
// delete All
question.clear();
// loop
question.forEach((value,key)=> console.log(`this ${key}, and its set ot ${vlaue})
// Another Way to Loop
for(let[key, value]of question.entries()){
console.log(`this ${key}, and its set ot ${vlaue});
if(typeof(key)=== 'number'){
console.log(`Answer ${key}, and its set ot ${vlaue});
}
</pre>
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==
==[[#top|Back To Top]]-[[Main_Page| Home]] - [[Java Script|Category]]==

Latest revision as of 23:36, 2 December 2016

ES6 Spread Operator

ES6 Equivalent Spread Operator

const sum3 = addFourAges(...ages);
// the ... means the ages array is expanded into individual components.

Join two Arrays

const arrayOne= ['hello', 'two'];
const arrayTwo = ['three', 'four'];
const arrayThree = [...arrayOne, ...arrayTow];

Looping through all elements and changing the color

const h = document.querySelector(h1)
const boxes = documwnt.querySelectorAll('.box');
const all = [h, ...boxes];
Array.form(all).forEach(cur=> cur.style.color='purple') // returns an array

Rest Parameter

function isFullAge(...years){
years.forEach(cur=> (216-cur)>=18);
}
 isFullAge(1990,1921,1967);

Default Parameters =

function Person(first, last = 'bacchas'){
this.firstname = first;
this.lastname = last;

}

Maps

New key value data structure

const question = new Map();
question.set('question', 'what is');
question.set(1,'es5);
question.set(2, 'es6');
question.set(3, 'ES2015');
question.set('correct', 3);
question.set(True, 'Correct Answer');
question.set(false, 'wrong');

// Retrieve DAta
question.get('question')
// get size
question.size;

// delete 
question.delete(2);

// delete All
question.clear();

// loop

question.forEach((value,key)=> console.log(`this ${key}, and its set ot ${vlaue})

// Another Way to Loop
for(let[key, value]of question.entries()){
console.log(`this ${key}, and its set ot ${vlaue});
if(typeof(key)=== 'number'){
console.log(`Answer ${key}, and its set ot ${vlaue});
}

Back To Top- Home - Category