Giter Site home page Giter Site logo

javascript-test's Introduction

JavaScript test

What will be printed to the console?

Task 1

function loop() {
  console.log(i);
  for (var i = 0; i < 5; i++) {
    console.log(i);
  }
  console.log(i);
}

loop();

Task 2

for (var i = 0; i < 5; i++) {
  setTimeout(function () {
    console.log(i);
  }, 1000);
}

Task 3

function override() {
  var a = 2
      b = 2;
}

var a = 1
    b = 1;

override();

console.log(a);
console.log(b);

Task 4

function printNames() {
  var names = ['John', 'Yoko', 'Elvis'];
  for (name in names) {
    console.log(name);
  }
}

printNames();

Task 5

function getUser() {
  return
  {
    name: 'John'
  };
}

var user = getUser();
console.log(user);

Task 6

console.log(1);
setTimeout(function () {
  console.log(2);
}, 0);
console.log(3); 

Task 7

var fruits = ['apple', 'banana'];
fruits.length = 1;
console.log(fruits);

Task 8

var obj = { x: 1 };
obj.y = obj = { x: 2 };
console.log(obj.y);

Task 9

var strings = ['10', '10', '10'];
console.log(strings.map(parseInt));

Task 10

console.log(9999999999999999);
console.log(0.1 + 0.2);
console.log(012);

Task 11

function Person(name) {
  this.name = name;
  this.sayName = function () {
    setTimeout(function () {
      console.log(this.name);
    }, 500);
  };
}

var person = new Person('John');
person.sayName();

Task 12

function Person(name) {
  this.name = name;
}

var name = 'Yoko';
var person = Person('John');
console.log(name);

Task 13

for (var i = 0; i < 3; i++) {
  subLoop();
}

function subLoop() {
  for (i = 0; i < 3; i++) {
    console.log(i);
  }
}

Task 14

function List() { };

List.prototype = {
  items: [],
  addItem: function (item) {
    this.items.push(item);
  }
};

var listA = new List();
var listB = new List();

listA.addItem('a1');
listB.addItem('b1');
listA.addItem('a2');

console.log(listA.items.length);
console.log(listB.items.length);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.