check if a javascript function is exist or is defind [duplicate] - javascript

This question already has answers here:
How to tell if a JavaScript function is defined
(23 answers)
Closed 9 years ago.
I want to know whether a function is exist or is defined before i call it.
I have this line of code:
function renderView(){
// some code
}

You can check the typeof, it returns undefined if it's not defined or out of scope, and function if it's a function
if ( typeof renderView == 'function' ) {
// it exists
}

Related

Does onclick = function put that function in a new scope? [duplicate]

This question already has answers here:
Why JavaScript function declaration (and expression)?
(5 answers)
Why use named function expressions?
(5 answers)
Closed 2 years ago.
Need your assistance. I have a function that was defined in onclick event and when i try to call it later there is an error that says function undefined.
author.onclick = function authorSlideInfo(event){
// do work
}
authorSlideInfo.then(() => {
// do work
}
Function returns a promise and i want to do something else after it ends. Is my function not in global scope? How to overcome this?

How can i check if an object is undefined (javascript)? [duplicate]

This question already has answers here:
JS checking deep object property existence [duplicate]
(2 answers)
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
(47 answers)
Closed 5 years ago.
I have to check if a object is undefined but when i do
typeof myUnexistingObject.myUnexistingValue == 'undefined'
i get this error
Uncaught ReferenceError: myUnexistingObject is not defined
so, how can I check for undefined obects or properties?
You must check for each potentially defined property before using it:
function checkUnexistingObject(myUnexistingObject) {
if (myUnexistingObject !== undefined) {
if (myUnexistingObject.otherObject !== undefined) {
console.log("All is well");
}
}
}
checkUnexistingObject({});
checkUnexistingObject({otherObject: "hey"});

Shortcut for accessing member of an object [, of an object...] [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 6 years ago.
Is there a short way for accessing a nested object without checking each parent object?
this will throw an exception if foo or bar is undefined:
var exists = (foo.bar.value !== undefined);
I would expect a check function like:
var exists = Object.exists(foo.bar.value);
Is there already something build-in javascript?
Use typeOf
if (typeof myObject!= "undefined") {
console.log('It exists')
}

How can I check if foo exists? [duplicate]

This question already exists:
How can I check whether a variable is defined in JavaScript? [duplicate]
Closed 9 years ago.
if (foo) {
// code to run if foo exists
}
results in Uncaught ReferenceError: foo is not defined
So how am i supposed to check if foo exists?
There's several ways - one is
if(typeof foo !== 'undefined'){
// your code here.
}
JS FIDDLE to play with to check

How can I check the variable whether it exists or not [duplicate]

This question already has answers here:
Detecting an undefined object property
(50 answers)
How to check a not-defined variable in JavaScript
(15 answers)
Closed 9 years ago.
var a;
typeof(a);
//undefined
typeof(c);
//undefined
if(c) {}
//throw error
How can I know that c doesn't exist without try catch.
Update after marked duplicate:
typeof initializedVariable and typeof notInitializedVariable both will show 'undefined'. My question is to know whether the variable exists(initialized) or not.
You can use the typeof operator.
if (typeof a === 'undefined') {
// variable is undefined
}

Categories

Resources