0

I tried to execute this code:

html code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="variables.js"></script>
</head>
<body>
    
</body>
</html>

js code:

const var1 = 10;
const var2 = 'Hello world';
const var3 = true;

const foo = function(){
    console.log("Whats up !!");
}();

foo;

console.log(typeof(foo));

When I am opening this javascript file and executing this javascript over browser chrome devtools inspector , its neither letting me create the founction foo(), nor is it letting me use foo().

I am not able to figure out the reason for it. Help is appreciated.

Tried to execute js in chrome dev tools.

Did not worked for me.

Error code:

enter image description here

1
  • foo is not a function in your code. Commented Jan 16, 2023 at 23:48

1 Answer 1

1

When you created a 'foo' function you called it. So your function is IIFE(Immediately Invoked Function Expression), that's why your 'foo' is undefined, because your IIFE called and returned undefined.

just don't call the function when you assign it to a variable.

const var1 = 10;
const var2 = 'Hello world';
const var3 = true;

const foo = function(){
    console.log("Whats up !!");
};

foo();

console.log(typeof(foo));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.