General Interview questions in Java script and node js

in interviewquestions •  6 years ago 
  1. How can we declare global variables in node js ?
    Global variable in the node js we can use global object to set any as global variable
    for example:
    file name: foo.js
    global.a = 10

    file name: foo1.js
    console.log(global.a)

  2. Addition of two arrays ?

    var b = [3, 4]
    var sum = 0
    if (b.length > a.length) {
        for (var i = 0; i < b.length) {
            temp = a[i] + b[i]
            sum = sum + temp
        }
    }
    else {
        for (var i = 0; i < a.length) {
            temp = a[i] + b[i];
            sum = sum + temp;
        }
    }
    console.log(sum)
    
  3. list any 5 functions of Array ?

    • shift
    • unshift
    • filter
    • push
    • pop
  4. difference between shift and unshift functions ?
    * The shift method removes the element at the zeroeth index and shifts the values at consecutive indexes down, then returns the removed value.If the length property is 0, undefined is returned.

    var a = [1, 2, 3, 4]
    a.shift()
    console.log(a)
    

    output:[2,3,4]

    • The unshift method inserts the given values to the beginning of an array - like object.
      unshift is intentionally generic; this method can be called or applied to objects resembling arrays.Objects which do
      not contain a length property reflecting the last in a series of consecutive, zero - based numerical properties may not
      behave in any meaningful manner.

      var a = [1, 2, 3, 4]
      a.unshift(-1, 0, 1]);

    output:[-1,0,1,1,2,3,4]

  5. what is closure in java script ?
    A closure is an inner function that has access to the outer(enclosing) function’s variables—scope chain.The closure has three scope chains: it has access to its own scope(variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

    The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters.Note that the inner function cannot call the outer function’s arguments object, however, even though it can call the outer function’s parameters directly.

  6. Convert array to string in java script eg var a = [1, 2, 3, 4] output: str = '1,2,3,4'

    var str = ''
        for (var i = 0; i < (a.length - 1); i++) {
            str = str + a[i] + ","
        }
        str = str + a[a.length - 1]
    
  7. delete key value from json object ?

    eg var a = { firstName: "Hari", lastName: "Prasad" }

    suppose i want to delete lastName from json Object access

    delete a.lastName;

  8. different data types in Java scripts ?

    • The latest ECMAScript standard defines seven data types:

      Six data types that are primitives:

      • Boolean
      • Null
      • Undefined
      • Number
      • String
      • Symbol(new in ECMAScript 6)

    and
    Object

  9. Concatenate arrays in java script ?

    var hege = ["Cecilie", "Lone"];
       var stale = ["Emil", "Tobias", "Linus"];
       var children = hege.concat(stale);
    
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!