-1

I'm trying to use strict mode in chrome console according to what I read these two method should work but only Method 1 is working. Is in Method 2 I'm doing something wrong or it is not possible to use strict mode like this.

Method 1:

(function f(){
    'use strict';
    function fn(text){herd=text; console.log(herd);}
    fn("Hi Welcome");
    })();

output of Method1

VM1139:3 Uncaught ReferenceError: herd is not defined
    at fn (<anonymous>:3:23)
    at f (<anonymous>:4:1)
    at <anonymous>:5:3

Method 2:

'use strict';
 function fnete(text) {as=text; console.log(as);}


'use strict'; fnete("hi");

output of method 2

hi

Attaching Image of error. enter image description here

Naveen
  • 55
  • 1
  • 11

1 Answers1

1

I think the problem in Method 2 is that your variable name is as which is a keyword.

Try using the same variable name herd in Method 2 which might fetch the same result as Method 1.

emptyjayy
  • 494
  • 4
  • 10
  • Then the browser would complain about that. But the OP is asking why **no** error is thrown in a case where the OP would expect that an error is thrown. – t.niese Mar 18 '17 at 13:46
  • 1
    @Naveen if it didn't show an error before when you used `as`, but now shows an error if you change it to `herd` then the resone is somewhere else. Maybe the page where you run your command in did define `as` already. – t.niese Mar 18 '17 at 14:23
  • yes @t.niese you are right. When I did that on new page it worked fine. – Naveen Mar 18 '17 at 15:10