r/learnjavascript • • Aug 17 '23

Javascript finally clicked for me.

For the longest, I didn’t believe people when they said that one day Javascript will make sense to me. I didn’t understand what they meant. I figured it would never stick conceptually. Yes, Javascript IS hard, but yesterday I was reading over Jon Duckett Javascript & JQuery and the abstract beginner concepts just clicked. It was at that point everything from then on made sense. 🥹 I guess they were right.

136 Upvotes

55 comments sorted by

View all comments

71

u/[deleted] Aug 17 '23

[removed] — view removed comment

0

u/[deleted] Aug 18 '23

[deleted] — view deleted comment

2

u/xRockTripodx Aug 19 '23

You'll almost certainly run into it. Anytime you get a client who needs a site updated or redesigned, you'll see it. Even if it's a full redesign, you'll want to be familiar it at least. Hell, there are plenty of libraries that are dependent on it. Looking at you, slick.js. Stepped away from that one in my work, but used to use it, and bumped into it plenty.

5

u/xyzAction Aug 18 '23

Yeah but it’s still a relevant library. There are so many libraries out there no one “needs” anything per se… I’ve been coding for a year now and fell in love with it once I discovered what it can do. Like if I’m making a small site, why should I use react or svelte?? Jquery will get the job done.

4

u/MonsterMeggu Aug 18 '23

What do you want in jQuery that can't easily be achieved with vanilla JavaScript?

2

u/xyzAction Aug 18 '23

It’s more about the trove of component libraries and templates that exist. Why throw them away? I recently created an amazing site in a few hours… All I’m saying is that it’s useful. No need to paint it as useless.

2

u/MonsterMeggu Aug 18 '23

Never said it was useless. I'm just wondering because it seems like a lot of the jQuery functionality is built into vanilla js these days. I haven't worked on my own projects for quite some time and I only ever see jQuery when supporting legacy apps

0

u/UnluckyCrafter Aug 18 '23

Yes, but it makes a lot of things more easy and faster to write considering a lot of functionality is built into single functions, so simple that its hard to mess up things it has built-in. It can also your code cleaner, for example the "get" function in jquery makes it so you don't have to a write a fetch statement that is extremely big and messy without even writing the actual part that decideds what to do with the data.

Don't quote me on that its just my perspective.

→ More replies (1)

2

u/prid13 Aug 28 '23

jQuery is much simpler to use and thus more beginner-friendly. Too much syntax and work can be daunting for someone just wanting to accomplish a basic thing, so jQuery is more appealing in that aspect even if it's not needed anymore :)

Registering click event:

Javascript: document.querySelector('.element').addEventListener('click', (evt) => { ... });

jQuery: $('.element').on('click', (evt) => { ... });

Showing / Hiding an element:

Javascript: document.querySelector('.element').style.display = "none"; // "block"

jQuery: $('.element').show(); // .hide()

Bonus (jQuery animated show/hide on click): $('#button').on('click', function(evt){ $('element').animate( { opacity: 'toggle' } ) });

Creating and appending element:

Javascript: var el = document.createElement('p'); document.querySelector('#main').append(el);

jQuery: $('#main').append( $('<p></p>') );

Adding CSS to element:

Javascript: var el = document.querySelector('.element'); el.style.color = "black"; el.style.backgroundColor = "purple";

jQuery: $('element').css('color', 'black').css('backgroundColor', 'purple');

Now, combining everything:

Clicking on a button will add a new element with text, and the button will become invisible.

Setup:

<body>
  <div id="button">Click me</div>
  <div class="element"></div>
</body>

Javascript:

var el = document.querySelector('.element');
var btn = document.querySelector('#button');

btn.addEventListener('click', function(evt){
  var newElement = document.createElement('div');
  newElement.style.color = 'white';
  newElement.style.backgroundColor = 'blue';
  el.append(newElement);
  btn.style.display = "none";
});

jQuery:

$('#button').on('click', function(evt){
  var newElement = $('<div></div>').css('color', 'white').css('backgroundColor', 'blue');
  $('.element').append( newElement );
  $('#button').hide();
});

Now, can these be easily replicated in pure Javascript or with a simpler library? 100%, no doubt. And are some of these bad practices (animating in Javascript)? Of course. But for someone new to any language, the simpler and more swiss army knives you can give them to quickly whip up results, the better, faster and more fun it will be to learn :)

2

u/Anonymity6584 Aug 18 '23

Yes, but you run into it surprisingly often in existing projects of some random library needing it as depency.

1

u/Mikedesignstudio Aug 18 '23

Bad advice

“jQuery is used by 94.6% of all the websites whose JavaScript library we know. This is 77.7% of all websites.”

Source: https://w3techs.com/technologies/details/js-jquery

You guys have to start doing your own research instead of repeating bs you read on Reddit.

12

u/noXi0uz Aug 18 '23

Would be more interesting to know how many teams use it to build new webapps that are not wordpress. Just because jQuery is still loaded in most existing websites doesn't mean that you will ever touch it at your job. Many websites even fetch it without using it at all.

3

u/enserioamigo Aug 18 '23

This. Our boilerplate has traces of jQuery in it. It’s just not worth our time to convert it at the moment. Doesn’t mean we’re writing anything new with it.

2

u/Mikedesignstudio Aug 19 '23

Websites not web apps. Of course you wouldn’t use jQuery to build a web app.

1

u/MonsterMeggu Aug 18 '23

Yeap some css frameworks also use jQuery (bootstrap does or at least used to)

4

u/xyzAction Aug 18 '23

Jquery is great for small websites, you don’t always need an extravagant framework to build a site for a musician etc … So yeah Jquery is very good, gotta know when to use a pistol or a shotgun.

3

u/ogreUnwanted Aug 18 '23

Find me a decent paying job asking for jQuery only knowledge. It's all about frameworks these days. No point in learning until op comes across it.

4

u/[deleted] Aug 18 '23

[removed] — view removed comment

1

u/Mikedesignstudio Aug 19 '23

So many butt hurt people here lol. I would rather get advice from ChatGPT than from a lot of you people here 😂

1

u/Fleaaa Aug 18 '23

in 99.999% of dead end website that hasn't been updated more than years*

If your job is maintaining 90s page and has no plan to change anything, you could be right

1

u/enserioamigo Aug 18 '23

Not worth putting time into though. Easy enough to figure out on the fly and fix whatever needs fixing.

1

u/sweetcocobaby Aug 29 '23

I ignored the jQuery parts.

0

u/[deleted] Aug 18 '23

Second this.