Search code examples
javascriptjquerytwitter-bootstrapbrowserify

jquery single variable declaration


I have a problem related to variable declaration. I use browserify, so here I have to require jquery before bootstrap.js. The issue is that I can't undestand what is the problem with the first variant of code?

var $, jQuery;
$ = jQuery = require('./libs/jquery/jquery.min');
require('./libs/bootstrap/bootstrap');

and

var $ = jQuery = require('./libs/jquery/jquery.min');
require('./libs/bootstrap/bootstrap');

First variant doesn't work (console shows me an error that bootstrap needs jquery), second variant is not good enough for Jshint but it works great! Can someone explain why the first variant doesn't work


Solution

  • Here's your answer:

    Your first variant creates no global variables:

    var $,jQuery
    

    created two variables - $, and jQuery in the local scope.

    The second one:

    var $ = jQuery = ...
    

    created one global variable jQuery and one local variable $. The first variant doesn't work because bootstrap is looking for the global variable jQuery and it can't find it.