0
Why JavaScript use “use strict” directive in programming?
Why JavaScript use “use strict” directive in programming?
'use strict' it is a literal expression. this expression is not present in earlier version javascript. Main purpose of this expression, you can't access variable, function, objects without declare.
Syntax
<script>
'use strict'; // mandatory
statement
</script>
'use strict';
x = 3.14; // This will cause an error because x is not declared
'use strict';
myFunction();
function myFunction() {
y = 3.14; // This will also cause an error because y is not declared
}
x = 3.14; // This will not cause an error.
myFunction();
function myFunction() {
'use strict';
y = 3.14; // This will cause an error
}
Example
<!DOCTYPE html>
<html>
<body>
<h2>With 'use strict':</h2>
<script>
'use strict';
function x(p1, p1) {
} // This will cause an error
</script>
</body>
</html>
