var bark = function() {   var animals = "Dogs";   var sound   = "Bark"   console.log(animals + " go " + sound); };  console.log(animals); console.log(sound);   When I log the variables to console, no cigar...  ReferenceError: animals is not defined  ReferenceError: sound is not defined
122
What is the scope of variables in javascript? Do they have the same scope inside as opposed to outside a function? Or does it even matter? Also, where are the variables stored if they are defined globally?
122496
Is there a way to find JavaScript variable on the page (get it as an object) by its name? Variable name is available as a string constant.
115876
I have this:  function foo($a='apple', $b='brown', $c='Capulet') {     // do something }   Is something like this possible:  foo('aardvark', &lt;use the default, please&gt;, 'Montague');
54195
Does javascript use immutable or mutable strings? Do I need a "string builder"?
119860
A weird bug was occurring in production which I was asked to look into. The issue was tracked down to a couple of variables being declared within a For loop and not being initialized on each iteration. An assumption had been made that due to the scope of their declaration they would be &quot;reset&quot; on each iteration. Could someone explain why they would not be)? (My first question, really looking forward to the responses.) The example below is obviously not the code in question but reflects the scenario:  Please excuse the code example, it looks fine in the editor preview?? for (int i =0; i&lt; 10; i++) {     decimal? testDecimal;     string testString;      switch( i % 2  )     {         case 0:         testDecimal = i / ( decimal ).32;         testString = i.ToString();             break;         default:             testDecimal = null;             testString = null;             break;     }      Console.WriteLine( &quot;Loop {0}: testDecimal={1} - testString={2}&quot;, i, testDecimal , testString ); }   EDIT: Sorry, had to rush out for child care issue. The issue was that the prod code had was that the switch statement was huge and in some &quot;case&quot;'s a check on a class' property was being made, like if (myObject.Prop != null) then testString = myObject.Stringval... At the end of the switch, (outside) a check on testString == null was being made but it was holding the value from the last iteration,hence not being null as the coder assumed with the variable being declared within the loop. Sorry if my question and example was a bit off, I got the phone call about the day care as I was banging it together. I should have mentioned I compared IL from both variables in and out the loop. So, is the common opinion that &quot;obviously the variables would not be reinitialized on each loop&quot;? A little more info, the variables WHERE being initialized on each iteration until someone got over enthusiastic with ReSharper pointing out &quot;the value is never used&quot; and removed them.  EDIT: Folks, I thank you all. As my first post I see how much clearer I should be in the future. The cause of our unexpected variable assignment can me placed on an inexperienced developer doing everything ReSharper told him and not running any unit tests after he ran a &quot;Code Cleanup&quot; on an entire solution. Looking at the history of this module in VSS I see variables Where declared outside of the loop and where initialized on each iteration. The person in question wanted his ReSharper to show &quot;all green&quot; so &quot;moved his variables closer to assignment&quot; then &quot;Removed redundant assignment&quot;! I don't think he will be doing it again...now to spend the weekend running all the unit tests he missed! How to do mark a question as answered?
4656
I have this piece of code (summarized)...  AnsiString working(AnsiString format,...) {     va_list argptr;     AnsiString buff;      va_start(argptr, format);     buff.vprintf(format.c_str(), argptr);      va_end(argptr);     return buff; }   And, on the basis that pass by reference is preferred where possible, I changed it thusly.  AnsiString broken(const AnsiString &amp;format,...) { ... the rest, totally identical ... }   My calling code is like this:-  AnsiString s1, s2;     s1 = working("Hello %s", "World");     s2 = broken("Hello %s", "World");   But, s1 contains "Hello World", while s2 has "Hello (null)". I think this is due to the way va_start works, but I'm not exactly sure what's going on.
4613
How can I create or use a global variable in a function?  If I create a global variable in one function, how can I use that global variable in another function? Do I need to store the global variable in a local variable of the function which needs its access?
53521
What JavaScript keywords (function names, variables, etc) are reserved?
96862
Aura Action javascript object has setCallback function that we use to communicate with the server. The first parameter of the function is scope. Aura documentation has the following explanation:  Scope : Object The scope in which the function is executed. You almost always want to set scope to the keyword this.  Experimenting with this keyword inside the component results in undefined when used inside controller.js and the helper object instance when used inside helper.js. Does someone know what the scope parameter is affecting? Using this feels wrong as it can be almost anything depending on the place in code the setCallback is executed from. P.S. currently I have problems with private attribute visibility inside the action callback function so I am trying to eliminate all possible causes.
58103
I was answering a question and this little inconsistency popped up.  Just wondering if anyone has come across it before, or if anyone knows why this happens.  A variable that is declared within some form of scoped element (if, for loop, etc.) shouldn't be available outside that scope.  i.e.  {     // accountNames variable is not available outside this scope     Set&lt;String&gt; accountNames = new Set&lt;String&gt; {'Test Account'}; }  system.debug(accountNames);   This debug statement fails with the message:      Variable does not exist: accountNames   However, if this is part of a query string being generated, the Database.query() call somehow has access to it.  String queryString = 'SELECT Id, Name FROM Account';  if(true) {     // accountNames variable is not available outside the `if` statement     Set&lt;String&gt; accountNames = new Set&lt;String&gt; {'Test Account'};     queryString += ' WHERE Name IN :accountNames'; }  // this debug will fail if you uncomment it // system.debug(accountNames);  // however the query somehow has access to it. // create an account called Test Account to prove it.  system.debug('queryString: ' + queryString); system.debug('queryString result: ' + database.query(queryString));   But, Salesforce is smart enough to know that it's not just some random string, so the following will fail:  // Alternatively, use a random string as the variable name, and salesforce  // is smart enough to know it's gibberish String queryString2 = 'SELECT Id, Name FROM Account';  if(true) {     queryString2 += ' WHERE Name IN :randomString'; }  try {     system.debug('queryString2 result: ' + database.query(queryString2));     system.assert(false); } catch (QueryException qe)  {     system.debug(':randomString is gibberish: ' + qe.getMessage()); }     Edit:  Interestingly, the :randomString is definitely calculated only when the query is executed.  String queryString3 = 'SELECT Id, Name FROM Account'; String queryString4 = 'SELECT Id, Name FROM Account';  if(true) {     // assign to query 3 before it has a value     queryString3 += ' WHERE Name IN :randomString';      // assign to query 4 after it has been declared     Set&lt;String&gt; randomString = new Set&lt;String&gt;{'Test Account'};     queryString4 += ' WHERE Name IN :randomString'; }  // both queries return correct results, regardless of when the variable was declared system.debug('queryString3 result: ' + database.query(queryString3)); system.debug('queryString4 result: ' + database.query(queryString4));   But it's not an open slather.  The following throws an error:  String queryString = 'SELECT Id, Name FROM Account WHERE Name = :someField'; someMethod(); system.debug('queryString result: ' + database.query(queryString));  void someMethod() {     String someField = 'Test Account';     system.debug('someField: ' + someField); }     Does anyone know why this is the case, or have an explanation why this would work?  As far as I can tell, at the time that the accountNames variable is in scope, no part of the code knows that it's anything other than a string.
31644
I'm looking for a clear, concise and accurate answer.    Ideally as the actual answer, although links to good explanations welcome.  This also applies to VB.Net, but the keywords are different - ByRef and ByVal.
53471
Now that it's clear , there is an associated concept that I use all the time without knowing what it really means.   I suppose everybody made once a mistake with parenthesis, resulting  in an "object is not callable" exception. What's more, using __init__ and __new__ lead to wonder what this bloody __call__ can be used for.  Could you give me some explanations, including examples with the magic method ?
54647
Given two controller methods defined for a Lightning component as follows, how can I call bar() from foo()? If I try it using the code shown below, I get the error, "bar is not defined".  ({     bar : function(component, event, helper) {         console.log('bar just happened');     },     foo : function(component, event, helper) {          // I would love for this to work,         // but it doesn't work as expected.         bar(component, event, helper);          // I've also tried this but get a slightly          // different but equally discouraging error.         this.bar(component, event, helper);     } })
31492
How do I enumerate the properties of a JavaScript object?  I actually want to list all the defined variables and their values, but I've learned that defining a variable actually creates a property of the window object.
107998
Is it like...  var obj = new Object();  obj.function1 = function(){     //code }   or something like that?
10974
I'm using Drupal 8.  What I'm trying to do is:   Access the active node From the node get the value of field_test and assign it to a variable Pass the variable to the drupalSettings function and subsequently to a custom module 'mycustommodule' that triggers an alert   In my content type, I created a field_test field, which is the field I reference in my code. The block is placed on a page of this content type.  This is the JavaScript code I am using.  mycustomblock.libraries.yml  custom:   version: 1.x   js:     js/custom.js: {}   dependencies:     - core/jquery     - core/drupalSettings   custom.js  (function ($, Drupal, drupalSettings) {   Drupal.behaviors.mycustomblock = {     attach: function (context, settings) {       var testMe = drupalSettings.mycustomblock.test;       alert(testMe);    } }; })(jQuery, Drupal, drupalSettings);   The error I get from the JavaScript console is this.    The error message is referring to this line.     Why is the instance of Drupal in the closing parenthesis throwing this error?
24071
I'm trying to understand Python's approach to variable scope. In this example, why is f() able to alter the value of x, as perceived within main(), but not the value of n?  def f(n, x):     n = 2     x.append(4)     print('In f():', n, x)  def main():     n = 1     x = [0,1,2,3]     print('Before:', n, x)     f(n, x)     print('After: ', n, x)  main()   Output:  Before: 1 [0, 1, 2, 3] In f(): 2 [0, 1, 2, 3, 4] After:  1 [0, 1, 2, 3, 4]