In your MODULENAME.module file:
$testVariable = 'himanshu'; drupal_add_js(array('MODULENAME' => array('testvar' => $testVariable)), array('type' => 'setting')); drupal_add_js(drupal_get_path('module', 'MODULENAME') . '/MODULENAME.js');
In MODULENAME.js file:
(function($) { Drupal.behaviors.MODULENAME = { attach: function (context, settings) { alert(settings.MODULENAME.testvar); } }; })(jQuery);
this way you can pass your php variable in drupal javascript file and use the variable 🙂
How can I implement the above in Drupal 8?