JavaScript Interview Questions Prototype and Object

What is JavaScript?, What are the features of JavaScript ?, Is JavaScript a case-sensitive language?, How can you create an object in JavaScript?
             What are the advantages of JavaScript?, What are the disadvantages of JavaScript?, How can we read the properties of an object in JavaScript?,What are JavaScript Data Types?

1. What are undeclared and undefined variables ?

Undefined: A variable has been declared but value has not been assigned.  

Undeclared: when you try to access a variable, which is not initialized or declared earlier.

2.What is an Array in JavaScript?

An array is a variable, which can store more than one value at a time.

3. How to create an array in JavaScript?

 

You can create array object using 3 ways.

  • using array literal
  • using creating an instance of Array
  • using using an Array constructor
  

        var arr1=[101,201,301]  //  using array literal
        var arr2 = new Array();  //•    using creating an instance of Array
        var  arr3 = new Array(101,201,301);  //using using an Array constructor

        console.log(arr1);  // output: [ 101, 201, 301 ]
        console.log(arr2);  // output []
        console.log(arr3); // [ 101, 201, 301 ]
    

4. what is prototype in JavaScript?

The prototype is an internal property  of an object and its simply refer to another object. You can add new properties or method to existing type using prototype.

Prototype is a global property which is linked with most of JavaScript objects except primitive type

 JavaScript Interview Questions, What is JavaScript?, What are the features of JavaScript ?, Is JavaScript a case-sensitive language?, How can you create an object in JavaScript?
             What are the advantages of JavaScript?, What are the disadvantages of JavaScript?, How can we read the properties of an object in JavaScript?,What are JavaScript Data Types?

In above code snippet, create a new object employee using browser console, __proto__ is linked with employee object and it a prototype object which is linked with employee object have predefined methods and properties like toString(), valueoOf() etc.

5. What is prototype inheritance in JavaScript?

 All JavaScript object inherit the methods and properties from prototype.

Date object inherit from Date.prototype

Array object inherit from Array.prototype

 String object inherit from String.prototye

The Object.prototype is on the top of the all prototype inheritance chain

6. What is __ proto __?

__proto__ is an actual object and it is used in the lookup chain to resolve methods. All objects have prototype. Prototype property is used by the JavaScript for inheritance.

 JavaScript Interview Questions, What is JavaScript?, What are the features of JavaScript ?, Is JavaScript a case-sensitive language?, How can you create an object in JavaScript?
             What are the advantages of JavaScript?, What are the disadvantages of JavaScript?, How can we read the properties of an object in JavaScript?,What are JavaScript Data Types?

7. What is the difference between prototype inheritance and class inheritance?

JavaScript Inheritance is limited to class that inheriting from other classes while prototypal inheritance supports the cloning of any object using an object linking mechanism. A prototype acts as a template for other objects, whether that is extending the base object or not.

8. What is default prototype methods of Object?

  • prototype.__defineGetter__()
  • prototype.__defineSetter__()
  • prototype.__lookupGetter__()
  • prototype.__lookupSetter__()
  • prototype.hasOwnProperty()
  • prototype.isPrototypeOf()
  • prototype.propertyIsEnumerable()
  • prototype.toSource()
  • prototype.toLocaleString()
  • prototype.toString()
  • prototype.unwatch()
  • prototype.valueOf()
  • prototype.watch()

9. How can you create new prototype method?

Use JavaScript prototype property to add new method to object constructor. Example

  

        function Employee(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        }

        // Added new method to Employee function
        Employee.prototype.fullName=function () {

        return `${this.firstName} ${this.lastName}`;

        }

        // create a object using employee function
        var empObj = new Employee('John', 'Done');

        var fullName =empObj.fullName();
        console.log(fullName); // output : John Done
    

10. How can you create new prototype property?

Use JavaScript prototype property to add new property to object constructor.
Example

  

        function Employee(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        }

        // Added new property country
        Employee.prototype.country=''

        // create a object using employee function
        var empObj = new Employee('John', 'Done');

        empObj.country ='Canada' ;
        console.log(empObj); // output : Employee { firstName: 'John', lastName: 'Done', country: 'Canada' }

    

11. How can you find all the properties and method of an object in JavaScript.

You can use Object.key(object) method to find all the properties of object.

  

        var employeeObj ={
        firstName:'John',
        lastName:'Doe',
        age:32,
        country:'Canada',
        fullName: function(){
        console.log(`full Name`);
        },
        salary:function(){
        console.log('print salary');
        }
        }

        var allKeys = Object.keys(employeeObj)
        console.log(allKeys);   // [ 'firstName', 'lastName', 'age', 'country', 'fullName', 'salary' ]

    

12. How can you find all the properties of an object in JavaScript.

You can use Object.key(object) method to find all the properties of object then filter the all keys and check the typeof key is not function

  

        var employeeObj ={
        firstName:'John',
        lastName:'Doe',
        age:32,
        country:'Canada',
        fullName: function(){
        console.log(`full Name`);
        },
        salary:function(){
        console.log('print salary');
        }
        }

        var allKeys = Object.keys(employeeObj)
        console.log(allKeys);   // [ 'firstName', 'lastName', 'age', 'country', 'fullName', 'salary' ]

        let propertiesKeys  = allKeys.filter(key => typeof employeeObj[key] != 'function');

        console.log(propertiesKeys);  // output : [ 'firstName', 'lastName', 'age', 'country' ]

    

12. How can you find all the methods of an object in JavaScript.

You can use Object.key(object) method to find all the properties of object then filter the all keys and check the typeof key is function.

  

        var employeeObj ={
        firstName:'John',
        lastName:'Doe',
        age:32,
        country:'Canada',
        fullName: function(){
        console.log(`full Name`);
        },
        salary:function(){
        console.log('print salary');
        }
        }

        var allKeys = Object.keys(employeeObj)
        console.log(allKeys);   // [ 'firstName', 'lastName', 'age', 'country', 'fullName', 'salary' ]

        let methodKeys  = allKeys.filter(key => typeof employeeObj[key] == 'function');
        console.log(methodKeys); // output : [ 'fullName', 'salary' ]

    

12. How to check property is exist in an object or not in JavaScript.

Use object.hasOwnProperty prototype method to check whether property exists or not. If exist, then it will return true otherwise it will return false.

  

        var employeeObj ={
        firstName:'John',
        lastName:'Doe',
        age:32,
        country:'Canada',
        fullName: function(){
        console.log(`full Name`);
        },
        salary:function(){
        console.log('print salary');
        }
        }

        let propertyExist = employeeObj.hasOwnProperty('firstName')
        console.log(propertyExist);  // true

        propertyExist = employeeObj.hasOwnProperty('name')
        console.log(propertyExist);  // false

    

13. What is the JavaScript native Objects?

JavaScript has many built-in or native objects and you can access these  objects   anywhere in your program.

  • Some of them are as follows:
  • Number Object
  • Boolean Object
  • String Object
  • Array Object
  • Date Object
  • Math Object
  • RegExp Object

14. What Is JavaScript Number Object?

The Number object represents numerical data and use for storing the either integers or floating-point numbers.
Syntax: you can create the number object as follows:

var numberObject = new Number(number)

15. What are the number properties?

JavaScript has many built-in or native objects and you can access these  objects   anywhere in your program.

  • Some of them are as follows:
  • Number Object
  • Boolean Object
  • String Object
  • Array Object
  • Date Object
  • Math Object
  • RegExp Object
  

        console.log(Number.MAX_VALUE);   // 1.7976931348623157e+308
        console.log(Number.MIN_VALUE);  // 5e-324
        console.log(Number.NaN);        // NaN
        console.log(Number.NEGATIVE_INFINITY);  // -Infinity
        console.log(Number.POSITIVE_INFINITY); //Infinity

    

16. What are the Number object methods?

 Number object method as follows:

  • toExponential(): returns as a string with a number rounded, written as exponential notation.
  • toFixed():returns as a string with the number written with a specified number of decimals.
  • toLocaleString(): Returns as a string value as a browser  local setting.
  • toPrecision():returns as a string with a number written with a specified length.
  • toString(): returns as a string.
  • valueOf(): returns value as a number.

17. What is new keyword in JavaScript?

new keyword is used for creating an object.

18. How do you remove a property from a JavaScript object?

Use delete operator to remove the property from object.

  

        var employeeObj ={
        firstName:'John',
        lastName:'Doe',
        age:32,
        country:'Canada',
        fullName: function(){
        console.log(`full Name`);
        },
        salary:function(){
        console.log('print salary');
        }
        }

        delete employeeObj.country;
        delete employeeObj['age']

        console.log(employeeObj);

        // output
        // {
        //     firstName: 'John',
        //     lastName: 'Doe',
        //     fullName: [Function: fullName],
        //     salary: [Function: salary]
        //   }
    

19. How do you remove a method from a JavaScript object?

Use delete operator to remove the method from object

  

        firstName:'John',
        lastName:'Doe',
        age:32,
        country:'Canada',
        fullName: function(){
        console.log(`full Name`);
        },
        salary:function(){
        console.log('print salary');
        }
        }

        delete employeeObj.fullName
        delete employeeObj['salary']
        console.log(employeeObj); // { firstName: 'John', lastName: 'Doe', age: 32, country: 'Canada' }
    

20. how can you get the value of object property without using dot(.)propertyname?

You can get the property value suing key name.

  

        var employeeObj ={
        firstName:'John',
        lastName:'Doe',
        age:32,
        country:'Canada',
        fullName: function(){
        console.log(`full Name`);
        },
        salary:function(){
        console.log('print salary');
        }
        }
        console.log(employeeObj['firstName']); // John
    

21. how can you convert string value to number without decimal places in JavaScript?

Use global JavaScript parseInt() method to convert number from string.

  

        var str1 ="101.32";
        var num1 = parseInt(str1);

        console.log(num1);
        console.log(parseInt("101.32"));
    

22. how can you convert string value to number with decimal places in JavaScript?

Use global JavaScript parseFloat() method to convert number with decimal places from string.

  

        var str1 ="101.32";
        var num1 = parseFloat(str1);

        console.log(num1); // 101.32
        console.log(parseFloat("101"));  //101
    

24. how to display number with 2 decimal places whether number has decimal place or not?

Use global JavaScript toFixed() method to show the number with decimal places.

  

        var num1 = 101;
        console.log(num1.toFixed(2));  //101.00
        var num2 = 101.1023;

        console.log(num2.toFixed(2));  // 101.10
    

25. how to check string is number in JavaScript?

Use global JavaScript isNaN() to check value is number or not.

  

        var str1 ='101';
        var str2 ='10 years'

        console.log(isNaN(str1));  // true
        console.log(isNaN(str2));  // false