Miscellaneous Operators offered by Javascripts

JavaScript supports a number of other miscellaneous operators

The Conditional Operator (?)

JavaScript conditional operator evaluates the first expression(operand), Based on the expression result return either second operand or third operand.

Syntax :

        
answer = expression ? answer1 : answer2;        
    
        
console.log((3 == 7) ? "Same value" : "different value");
//Output : different value        
    

In the above example expression checks the condition. If the condition is true then the left portion is called otherwise right portion is called. 

The typeof Operator

typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string that specifies the type of the operand. The following table specifies the value of the typeof operator for any JavaScript value

Variable Type of variable
undefined “undefined”
null "object"
true or false "boolean"
any number "number"
any string "string"
any function "function"

The following are examples to understand in detail.

        
var name = 'John';
var age = 30;
var married = true;
var job = ['Software','hardware','testing'];
var str = function(){ console.log("Hello"); }
var address;

typeof name;         // Output : "string"
typeof age;        	 // Output : "number"
typeof married;      // Output : "boolean"
typeof experience;   // Output : "object"
typeof str;          // Output : "function"
typeof address;    	 // Output : "undefined"        
    

Delete operator

delete is a unary operator that attempts to delete the object property or array element specified as its operand. If delete is not allowed then return false otherwise return true.

        
var experience = [2010, 2011, 2012, 2013];    

delete experience[2];                  
// delete 2nd index from array elements

console.log(experience);    
//Output: [2010, 2011, undefined, 2013, 2014]
        
    

The instanceof Operator

The instanceof operator returns true if the specified object is an instance of the specified object

        
const person = ["John", "cart", "Layfield"];

console.log(person instanceof Array);   //Output : true
console.log(person instanceof Object);  //Output : true
console.log(person instanceof String);  //Output : false
console.log(person instanceof Number);  //Output : false         
    

New Operator

JavaScript new operator to create an instance of the object.

        
var myObj = new Object();  // or you can write: var myObj = new Object;
myObj.name = "John carter";
myObj.address = "Washington DC.";
myObj.age = 25;
myObj.married  = false;

console.log(myObj);

//Output : Object { name: "John carter", address: "Washington DC.", age: 25,married: false }         
    

this Operator

JavaScript this operator represents a current object.

        
function person(name, address, age, married) {
   this.name = name;
   this.address = address;
   this.age = age;
   this.married = married;
}
var myObj = new person("John Carter", "Washington DC", 30, true);

console.log(myObj);
// Output : Object { name: "John Carter", address: "Washington DC", age: 30, married: true }        
    

in Operator

JavaScript in operator return boolean result if a specified property exists in the object.

        
var myObj = new Object();      
myObj.name = "John";
myObj.address = "New york";
myObj.age = 33;
myObj.married  = false;

console.log("name" in myObj);      // Output : true
console.log("birthdate" in myObj); // Output : false        
    

Leave a Comment

Your email address will not be published.

Scroll to Top