Skip to content

Instantly share code, notes, and snippets.

test('Default value expressions', (t) => {
t.plan(3)
function bar(val) {
return y + val;
}
function foo(x = y + 3, z = bar( x )) {
return [x, z];
test('es6 way of setting default parameters', (t) => {
t.plan(3)
function foo(x = 11, y = 31) {
return x + y;
}
let b = foo(5,6);
let c = foo();
import test from 'ava';
test('es5 way of setting default parameters', (t) => {
t.plan(3)
function foo(x, y) {
x = x || 11;
y = y || 31;
return x + y;
test('rest operator to gather all arguments', (t) => {
function foo(...args) {
t.same(args, [1,2,3,4,5]); //Compare args value to an array [1,2,3,4,5]
}
foo(1,2,3,4,5);
});
test('basic spread function with ... operator', (t) => {
t.plan(3);
function foo(x,y,z) {
t.is(x, 1); // true
t.is(y, 2); // true
t.is(z, 3); // true
}
foo(...[1,2,3]);
test('rest with multiple arguments', (t) => {
function foo(a, b, ...c) {
t.is(a, 1);
t.is(b, 2);
t.same(c, [3,4,5]);
}
foo(1,2,3,4,5);
test('Block scope functions', (t) => {
t.plan(1);
{
foo();
function foo() {
t.pass();
}
}
test('Mutate variables inside complex const variable', (t) => {
t.plan(1);
{
const a = [1,2,3,4];
a.push(5);
t.is(a, [1,2,3,4,5]);
}
test('Const variable before and after mutation attempt', (t) => {
// Expect two assertions
t.plan(2);
// Declare thee variables using let
const a = 2;
t.is(a, 2);
try {
a = 3; // immutable variable
} catch (e) {
test('Accessing let declared variable before being assigned', (t) => {
// Expect two assertions
t.plan(2);
// Declare thee variables using let
{
t.is(a, undefined);
t.is(b, undefined);
var a;