Skip to main content
Copy edited.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Self executing-executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.

The following code:

var same_name = 1;

var myVar = (function() {
    var same_name = 2;
    console.log(same_name);
})();

console.log(same_name);

produces this output:

2
1

By using this syntax you avoid colliding with global variables declared elsewhere in you javascriptyour JavaScript code.

Self executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.

The following code:

var same_name = 1;

var myVar = (function() {
    var same_name = 2;
    console.log(same_name);
})();

console.log(same_name);

produces this output:

2
1

By using this syntax you avoid colliding with global variables declared elsewhere in you javascript code.

Self-executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.

The code

var same_name = 1;

var myVar = (function() {
    var same_name = 2;
    console.log(same_name);
})();

console.log(same_name);

produces this output:

2
1

By using this syntax you avoid colliding with global variables declared elsewhere in your JavaScript code.

edited body
Source Link
Daniel
  • 1.5k
  • 13
  • 16

Self executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.

The following code:

var same_name = 1;

var myVar = (function() {
    var same_name = 2;
    console.log(same_name);
})();

console.log(same_name);

produces this output:

1
2
1

By using this syntax you avoid colliding with global variables declared elsewhere in you javascript code.

Self executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.

The following code:

var same_name = 1;

var myVar = (function() {
    var same_name = 2;
    console.log(same_name);
})();

console.log(same_name);

produces this output:

1
2

By using this syntax you avoid colliding with global variables declared elsewhere in you javascript code.

Self executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.

The following code:

var same_name = 1;

var myVar = (function() {
    var same_name = 2;
    console.log(same_name);
})();

console.log(same_name);

produces this output:

2
1

By using this syntax you avoid colliding with global variables declared elsewhere in you javascript code.

Source Link
Daniel
  • 1.5k
  • 13
  • 16

Self executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.

The following code:

var same_name = 1;

var myVar = (function() {
    var same_name = 2;
    console.log(same_name);
})();

console.log(same_name);

produces this output:

1
2

By using this syntax you avoid colliding with global variables declared elsewhere in you javascript code.