Madhukar
Madhukar

Reputation: 1

Issue executing javascript code on chrome dev-tools

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

Upvotes: 0

Views: 38

Answers (1)

Tatul Mkrtchyan
Tatul Mkrtchyan

Reputation: 371

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));

Upvotes: 1

Related Questions