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.
An array is a variable, which can store more than one value at a time.
You can create array object using 3 ways.
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 ]
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
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.
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
__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 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.
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
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' }
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' ]
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' ]
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' ]
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
JavaScript has many built-in or native objects and you can access these objects anywhere in your program.
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)
JavaScript has many built-in or native objects and you can access these objects anywhere in your program.
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
Number object method as follows:
new
keyword is used for creating an 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] // }
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' }
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
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"));
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
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
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