Expressions & Operators
delete
typeof
void
delete
Removes a property from an OBJECT
delete object.property
delete object['property']
OBJECT
name of an object, or an expression evaluating to an object
PROPERTY
the property to delete
The delete operator
- Returns FALSE in non-strict cases
- Returns TRUE in all other cases
The delete operator removes the PROPERTY from the object entirely.
BUT
if a property that has the EXACT SAME NAME exists on the object's prototype chain,
the OBJECT WILL INHERIT THAT PROPERTY from the prototype.
- delete is only effective on an object's PROPERTIES
- delete has NO EFFECT on variable / function names
- delete can't remove certain properties of predefined objects (like Object, Array, Math etc).
a = 33; // creates the property a on the global object
var b = 43; // declares a new variable, b
myobj = {
c: 5,
d: 3
};
// a is a property of the global object and can be deleted
delete a; // returns true
// delete doesn't affect variable names
delete b; // returns false
// user-defined properties can be deleted
delete myobj.c; // returns true
// myobj is a property of the global object, not a variable,
// so it can be deleted
delete myobj; // returns true
eg:
typeof
The typeof operator
- Returns a string indicating the type of the unevaluated operand.
typeof operand
OPERAND = an expression representing the object / primitive whose type will be returned
Return values of typeof
- Undefined
- Null
- Boolean
- Number
- String
- Symbol
- Host object
- Function object
- Any other object
"undefined"
"object"
"boolean"
"number"
"string"
"symbol"
Implementation-dependent
"function"
"object"
eg:
typeof 78 === 'number';
typeof 7.17 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number';
typeof "" === 'string';
typeof "javascript" === 'string';
typeof (typeof 7) === 'string'; // typeof always return a string
typeof true === 'boolean';
typeof false === 'boolean';
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof undefined === 'undefined';
typeof doopy === 'undefined';
typeof {a:1} === 'object';
typeof [1, 2, 4] === 'object';
typeof function(){} === 'function';
typeof null === 'object';
void
The void operator
- Evaluates the given expression and then returns undefined.
void expression
void allows the insertion of expressions that produce side effects into places where an expression that evaluates to undefined is DESIRED
void is used to obtain the undefined primitive value using "void(0)"
When a browser follows a javascript: URI, it EVALUATES the code in the URI and then REPLACES the contents of the page with the returned value, unless the returned value is undefined.
The void operator can be used to return undefined.
eg:
<a href="javascript:void(0);">
Click here to do nothing
</a>
<a href="javascript:void(document.body.style.backgroundColor='red');">
Click here for red background
</a>
expressions&operators
By mihaelaadln
expressions&operators
- 241