If you are using drupal_add_js()
in your form building function, you are attaching JavaScript to a page, and that's it.
If you are using $form['#attached']['js']
, other modules may interact, test and change it as they need, using hook_form_alter()
. That's the way you should choose every time your JavaScript is functionally related to particular form.
Other than that, they are functionally the same.
Note that it is usually better to use JavaScript libraries than plain JavaScript files, as they allows you to declare versions, keep relations between JS and CSS, manage dependencies, well, they are generally way more powerful.
Of course then you should use $form['#attached']['library']
when your JavaScript Library is functionally related to particular form, and only use drupal_add_library()
when no form is in any way related to library's function.