• 1. What will the code below output to the console and why?

    (function(){
      var a = b = 3;
    })();
    
    console.log("a defined? " + (typeof a !== 'undefined'));
    console.log("b defined? " + (typeof b !== 'undefined'));
    
    Answer:

    Result as below:

    a defined? false
    b defined? true
    

    Explain: In javascript provided 2 scopes related to declaring the variable. They are global scope and function scope so in this example, we can see:

    - a variable declared inside the function so Its scope is function scope so you can't access this variable outside function.

    - variable does not declare inside the function, it only assigns value inside the function so b variable is the global scope.

    *Note: This question wants to check the knowledge about the scope of variables in javascript.

  • 2. We have an object as below :

    var beNice = (function () {
        'use strict';
        var publicAPIs = {};
        var saySomethingNice = function (somethingNice) {
            alert(somethingNice);
        };
        publicAPIs.smile = function (message) {
            if (message) {
                saySomethingNice(message);
            } else {
                saySomethingNice('You make the world better just by being you!');
            }
        };
        return publicAPIs;
    })();

    And now we call 2 actions as below, please say what output of them and what is different between them?

    beNice.saySomethingNice();
    beNice.smile('hello world');
    
    Answer:

    1. beNice.saySomethingNice() This line code will throw an exception "saySomethingNice is not defined" because this function is a private function so you can not call it like a public function.

    2. beNice.smile('hello world') This line code will alert to "hello world" because it is a public function. 

    *Note: This question wants to check the knowledge relating to the private and public function

  • 3. What is the difference between undefined and null in javascript? Explain and example.

    Answer:

    1. undefined: It does not assignment value, it occurs when a variable has been declared but has not yet been assigned a value, for example:

    var age;
    console.log(typeof age); // undefined

    2. null : Is an assignment value, you can be assigned to a variable as a representation of no value. For example :

    var age = null;
    console.log(typeof age); // return object

    *Note: This question wants to check the knowledge about the data type in javascript.

  • 4.  What is 'this' keyword in JavaScript? 

    Answer:

    this keyword refers to the object it belongs to, it depends on where this keyword called.

    In a function, this refers to the global object.
    In a function, in strict mode, this is undefined.
    In a method, this refers to the owner object.
    In an event, this refers to the element that received the event.
    Methods like call(), and apply() can refer this to any object.

    For example :

    var employee = {
      firstName: "Quiz",
      lastName : "Dev",
      fullName : function() {
        return this.firstName + " " + this.lastName;
      }
    };

    In this example this keyword is called inside the method of the object so this refers to the "owner" of the method.

  • 5.  What is the difference between "==" and "===" operators in Javascript?

    Answer:

    When declaring a variable we have two parts they are data type and value.

    var age = 18; // dataType = number, value = 18 

    So == operator only compare value and === compare both value and datatype.

    var age = 18; // dataType = number, value = 18
    var myAge = "18"; // dataType = string, value = 18
    
    age == myAge; // True
    age === myAge // False  

    Refer difference between "==" and "==="

  • 6. What would be the result of 5+2*"10"?

    Answer:

    The result is 25.

    Because when performing multiplication, the string automatically cast to numeric so 2*"10" = 20 + 5 = 25.

    You can see a demo here.

  • 7. What is localStorage in Javascript? When we should use it?

    Answer:

    The localStorage is a new feature introduced in HTML5 and it is a type of web storage that allows websites to store and access data in the browser with no expiration date.

    localStorage stored value into a text file and save it on the client computer. Have some information about localStorage:
    - Do not store sensitive user information in localStorage.
    - It is not a substitute for a server-based database as information is only stored on the browser.
    - localStorage is limited to 5MB across all major browsers.
    - localStorage is quite insecure as it has no form of data protection and can be accessed by any code on your web page.
    - localStorage is synchronous, meaning each operation called would only execute one after the other.

    So when we should use it?
    - We only use to store data no need secure. such as: save the favorite car, count like ... of that user.

  • 8. How to remove any item of an array in Javascript? Provide an example.

    Answer:

    We have some ways to remove an item in an array

    1. using pop() method

    This method helps removes the last item from an array.

    var colors = ["red", "orange", "blue", "gray"];
    colors.pop(); // Removes the last element ("gray") from colors

    2. Using shift() method

    This method helps removes the first item from an array.

    var colors = ["red", "orange", "blue", "gray"];
    colors.shift(); // Removes the first element ("red") from colors

    3. Using delete keword

    See example :

    var colors = ["red", "orange", "blue", "gray"];
    delete colors[0];
    console.log(colors[0]); // undefined
    console.log(colors.length); // 4

    This keyword only removes item but not remove index of that item so when you check length it is still not changed. I do not recommend using this solution because it maybe leaves undefined holes in the array. Use pop() or shift() instead.

    4. Using splice() method

    This method is very helpful, you can add or remove any item you want by index.

    var colors = ["red", "orange", "blue", "gray"];
    colors.splice(0, 1); // remove one item start index = 0 (remove first item)
    colors.splice(2, 1); // remove one item start index = 3(remove item 3)
    
  • 9. How many ways to create an object in Javascript?

    Answer:

    In JavaScript, there are four methods to use to create an object:

    1. Object Literals.

    2. New operator or constructor.

    3. Object.create() method.

    4. Class.

  • 10. what is the cookie in javascript? When we should use it?

    Answer:

    Cookies are data, they are small items of data and store, each consisting of a name and a value. They are store on your computer. When a web server has sent a web page to a browser. In Javascript to has support cookie through document.cookie object

    Some short information about cookies in Javascript:

    - Stored in client-side

    - It can create and manipulation in server-side.

    - It can set expired time to remove data.

    When we should use it?

    Because cookies store in browser of the user so it is very helpful to check all activity of that user such as: login, save favorite something ...

    To get or set cookie you can refer article link below for more information:

    How to set, get and delete a cookie in Javascript

  • 11. What is regex in Javascript? When need to use it?

    Answer:

    regular expression is an object that describes a pattern of characters.

    Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text.

    Regex usually uses to validate some info such as email, phone, git code .... or if you have a book text with 500 pages, I want to search all phone number appears in this box, Use regex to find is the best Idea.

  • 12. What is the 'Strict' mode in JavaScript and how can it be enabled?

    Answer:

    As you know you can declare a variable with no need keyword before (var, const or let),  maybe no need a semicolon at the end of a command and maybe compare use == instead of === and many things other.

    So to fix all that problem, ES6 has provided  'Strict' mode to fix them.

    how to enable Strick mode?

    It is very simple to do, add "use strict" to the first your js.

    For example:

    "use strict";
    x = 3.14;    // This will not cause an error. x is not defined
    
  • 13. What is JSON in Javascript? And What are stringIfy() and parse() functions.

    Answer:

    JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for machines to parse and generate. JSON is often used when data is sent from a server to a web page.

    The JSON syntax is derived from JavaScript object notation syntax, but the JSON format is text only. The code for reading and generating JSON data can be written in any programming language.

    For example syntax:

    {
        "students":[
          {"FirstName":"Mixam", "LastName":"Doe"},
          {"FirstName":"Anna", "LastName":"Bro"},
          {"FirstName":"Peter", "LastName":"Jack"}
        ]
    }

     stringIfy() : Convert object or list object to string.

    parse() : Convert string  text to object.

  • 14. what is closure in javascript? Explain with an example

    Answer:

    A closure in Javascript is a combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). it gives you access to an outer function’s scope from an inner function. Closures are created whenever a function is created.

    For example:

    function makeAdder(x) {
      return function(y) {
        return x + y;
      };
    }
    
    var add5 = makeAdder(5);
    var add10 = makeAdder(10);
    
    console.log(add5(2));  // 7
    console.log(add10(2)); // 12
    
  • 15. What is the difference between .call() and .apply() in Javascript?

    Answer:

    call and apply are very similar. They invoke a function with a specified this context, and optional arguments. 

    Difference between call() and apply() method: The only difference is call() method takes the arguments separated by commas while apply() method takes the array of arguments.

    For example:

    function longerSummary(genre, year) {
      console.log(
        `${this.title} was written by ${this.author}. It is a ${genre} novel written in ${year}.`
      )
    }
    
    longerSummary.call(book, 'dystopian', 1932);
    // OR
    longerSummary.apply(book, ['dystopian', 1932]);
    
RATE