What does this symbol mean in javascript?

Asked

Viewed 169 times

-1

I’m new to javascript and came across a code snippet that contains the symbol of $ mixed with a structure of if and else.

What is its function individually, in addition to the logic gate function E?

  if (dayOfWeek === 6 || dayOfWeek === 0 || hour <= 9 || hour >= 17) {
        $('.hours').hide();
    }
    // closed any other time than above * working from 0am -9am but none other
    if (dayOfWeek === 6 || dayOfWeek === 0 || hour <= 0 || hour >= 9) {
        $('.closed').hide();
    }
  • 1

    Most likely is the jQuery

  • Javascript allows you to use almost any character as a variable name. It is quite common $ be used by the library jQuery as mentioned by hkotsubo

  • 1

    It may be jQuery, Zepto, or even a proper implementation, because personally I think jQuery is a poorly planned bomb, I created a clone of jQuery itself, only using a series of benchmark tests, and I also came to see other implementation, as well as mine, which were not popular, but very good... up to distributed implementations, only used on specific websites.

1 answer

3

That one $ a shortcut to using Jquery, which can also be run using jQuery. If you don’t know what it is, it’s a library javascript.

See in this example the operation:

$("#btn").click(function () {
    $('.um').hide();  // usando o atalho $
    jQuery('.dois').hide();  // usando jQuery
});
div { 
    height: 50px;
    width: 150px;
    margin-bottom: 20px;
    border: solid 1px #000;
    background-color: green
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div class="um"></div>
<div class="dois"></div>

<button id="btn">Clique aqui para testar</button>

  • 3

    It’s a great chance to be, but it’s not guaranteed to be. jQuery is one of the best known lib that uses the $prefix, but nothing guarantees that it is the author’s case. Even, jQuery himself mentions this and has a feature to avoid conflicts. https://api.jquery.com/jQuery.noConflict/ - highlight for the snippet Many Javascript Libraries use $ as a Function or variable name, just as jQuery does ... - more or less "Many JS libraries use $ as function name or variable, just like jQuery does ..."

  • 1

    yes @Bacco lacks a little context to state for sure that it is Jquery. I have worked with several frameworks, and only remember the prototype js (http://prototypejs.org/) that already used the $ as a shortcut, I even remember giving conflict with jQuery

Browser other questions tagged

You are not signed in. Login or sign up in order to post.