EDIT:
I fixed this, using: the module_name.settings.yml file under module_name/config/install folder and also by using the module_name.module file where I've implemented the hook_form_alter() method.
So,
From my module_name.module file, here goes my code:
/**
* Implements hook_form_alter().
*/
function modulename_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
if($form_id === your_form_id){
$form['#attached']['library'][] = 'module_name/route_name';
// that's the route name you've defined in your module_name.libraries.yml file
// where you've located the js file you're going to use
$form['#attached']['drupalSettings']
[module_name][route_name][send_this_variable_to_js] = \Drupal::config(module_name.settings)->get(key);
return $form;
}
}
Now in your JS file, you can console.log like this:
console.log(drupalSettings.module_name.route_name.server.send_this_variable_to_js)
Hope this help!