Strange Javascript Expression

if (1) { google_conversion_value = 1; } 

What is the meaning of the above? I mean, it seems like this will always be executed, so why bother with an if ?

updated: one of the reasons may be the rest of the server-side scripts. Any other ideas?

updated2: can easily change the value of an assignment without worrying about if , no?

+6
source share
8 answers

There are two possible explanations:

  • This is a disconnect from debugging.
  • A file containing this code is generated dynamically, and the source code of the source contains something like if(<?php echo $some_stuff_enabled; ?>)

However, in the latter case, it would be easier to display this code block only if the condition is met, but perhaps it is used in some shitty template that simply allows replacement, but no conventions ...

+4
source

I have seen this before, and I always assumed that these were the remains of some old condition that was no longer needed, but was never deleted. I see no real reason to do something like this otherwise.

+2
source

Perhaps the if statement is used to validate a legitimate conditional expression, and then someone replaced it with the right value for testing / debugging, etc.

You are right, it will always be executed, because 1 right. I would look at your version control history and examine this line to see if it is used to contain real conditional. If the condition has always been 1 , then this is most likely an expression about debugging. Otherwise, someone could mean that this is a temporary change and may not have been intended to test this (which may be bad).

+2
source

This is potentially because the person writing the code would like to easily turn it off and on again, this is especially useful if there is a lot of code in the block (not here).

Another possibility is that the original programmer could not worry about writing the logic, or rather, he was not specified, so the "if" remained as a placeholder.

+2
source

Most likely, there is a debug version or something similar left. You are right, this will always be done. It could also be done so that it can be easily turned on / off by setting if to 0. Perhaps the developer intended to use it as a flag somewhere else in the code?

+2
source

this actually happens when the if condition is output from the server, so instead of doing the right thing and not producing a script when the condition is false, they do something like this:

 if (<% if (my_server_condition) then Response.Write("1") else Response.Write("0") %>){ // code goes here } 
+2
source

I'm not sure where this code comes from, but as you pointed out, it will always execute. As for why you do this, there are times when you want to see what the result of the branch code will be without having to set up your environment. In this case, you can comment on the actual value and replace it with if(1) instead for testing:

 // if( ... some hard to achieve condition ) if (1) { // Now you can see what happens if this value is set quickly google_conversion_value = 1; } 

Of course, the problem is that it is sometimes easy to forget to remove if(1) and uncomment the proper state.

0
source

This is actually javascript recommended by Google at http://support.google.com/adwords/bin/answer.py?hl=en&answer=1722054#nocomments (click on Step 2 for sample HTML)

0
source

Source: https://habr.com/ru/post/887927/


All Articles