1

I know that JavaScript is a programming language while AngularJS is MVC based framework that is used with JavaScript.

But I have an assignment where I supposed to compare:

  • JavaScript vs jQuery syntax,
  • and JavaScript vs AngularJS syntax.

Now, for the JavaScript vs jQuery part, I easily compared them by showing the comparison between DOM methods and jQuery equivalent method and how the functions are minimized and how I can access different elements very easily and few lines of code using jQuery instead of JavaScript.

But in case of AngularJS vs JavaScript, how am I supposed to compare its syntax since AngularJS is a framework?

Is it even possible to compare two things that can be done in both JavaScript and AngularJS but pretty easy to be done using AngularJS ? Because as far as I understood, JavaScript and AngularJS are two separate things.

I am newbie in Web Development and have started developing like 3 weeks ago in HTML5, so pardon me for any mistake.

Riley Willow
  • 594
  • 2
  • 5
  • 21
  • 4
    To whoever gave you that assignment: that's just ridiculous. – Shomz Apr 25 '15 at 11:41
  • stop making puns, I know that was the ridiculous question to ask, but I am not the one who asked it, my teacher is. How possibly I could compare the syntax of two entirely different things ? – Riley Willow Apr 25 '15 at 11:46
  • We're sorry I guess, but that's just a very weird question, what are even the characteristics to compare by? There must be a gazillion other more constructive HW questions. – Omri Aharon Apr 25 '15 at 11:47
  • I am not asking for the answer to the question adn I know thats a weird question, I just need some kind of a point to start from, I neved have heard of AngularJS before I was given that assignment. Any kind of response to this question would be appericiated, at least it will help me to move on. – Riley Willow Apr 25 '15 at 11:49
  • In order to compare, you have to know at least on a basic level what Angular JS is. That's not something you learn in a couple of hours. I think that at the very least the question is not well defined. At least you need to ask him for some parameters. – Omri Aharon Apr 25 '15 at 11:51

3 Answers3

8

Angular and jQuery are both libraries/frameworks written in Javascript. Asking for me to compare Angular to Javascript is like asking for a comparison between bread and flour.

However, the browser APIs for DOM manipulation are implemented with a fundamentally different idea to Angular, so I'm going to assume that this is what your assignment is expecting you to identify.

The key point to focus on is that the paradigm is fundamentally different. Angular takes a declarative approach, whereas both the DOM and jQuery are imperative.

Declarative

A style of building the structure and elements of computer programs, that expresses the logic of a computation without describing its control flow. [1]

A declarative language is one in which you describe what you want, not how it should be done. This is very much Angular's approach with directives.

Imperative

[A style] in which algorithms are implemented in terms of explicit steps. [1]

When we write code using the DOM APIs or jQuery, we have to detail the "how" of the process.

Let's take data binding as an example. We want to bind a value from a text input, into a label.

With the DOM

<input type='text' id='bind-input'>
<label id='bind-output'></label>

And our Javascript:

window.addEventListener('load', function() {
  var input = document.getElement('bind-input'),
      output = document.getElement('bind-output');

  input.addEventListener('change', function() {
    output.innerText = input.value;
  });
});

We have to very specifically explain what we need the browser to do. We have to listen to certain events, make sure that the DOM is loaded, keep track of our element's IDs, all just to update our label whenever our input changes.

With jQuery

<input type='text' id='bind-input'>
<label id='bind-output'></label>

We still need some Javascript:

$(document).ready(function() {
  var $input = $('#bind-input'),
      $output = $('#bind-output');

  $input.on('change', function() {
    $output.html($input.value());
  });
});

As you can see, this is pretty similar to the DOM approach. We have to explain to the browser exactly what we want it to do.

With Angular

<input type='text' ng-model='bound'>
<label ng-bind='bound'></label>

We don't even need to write any code for this to work with Angular! (Obviously we'd need an empty controller somewhere, but you get the idea).

The declarative approach is primarily a way for us to abstract away from the implementation details. Angular directives make it very easy to wrap up these common behaviours into small reusable components than can be applied to our HTML in a declarative way.

Dan Prince
  • 29,491
  • 13
  • 89
  • 120
  • this is very good answer, thank you for that..although i have to read about so many concepts to completely understand it. So, if `jQuery` just making the writing of JavaScript code easier, is there anything that can be done in `JS` but not in `jQuery` ? or vice versa ? – Riley Willow Apr 25 '15 at 12:10
  • 1
    Well, jQuery is just Javascript except you have access to some extra objects and functions. That means whenever you are doing jQuery, you are also doing JS. There is nothing to stop you mixing it with the DOM approach. So the answer is __no__, because they are not mutually exclusive. – Dan Prince Apr 25 '15 at 12:13
4

Firstly, it appears you (or the person assigning the question) slightly misunderstands what jQuery/AngularJS are. jQuery and Javascript aren't mutually exclusive, and AngularJS and Javascript aren't mutually exclusive. Briefly:

  • Javascript: all code (er, that we care about in this question :-) that a web-browser can run in a web-page is written in Javascript
  • jQuery a library of functions written in Javascript that you can use in a web page. Writing something using jQuery is just writing something using Javascript, using the extra functions jQuery provides.
  • AngularJS a library of functions written in Javascript that you can use in a web page. Writing something using AngularJS is just writing something using Javascript, using the extra functions AngularJS provides.

(Note the similarities between my definitions of jQuery/AngularJS. Yes some people say AngularJS is a framework, but it's just a set of Javascript functions that people have written)

Secondly, I think the question is actually slightly under-specified. I would give yourself a standard task that is done in a web browser, such as

  • Fetch the last 20 items from a Twitter feed, and display the results in a web page

Using

  • Plain Javascript (no framework or library)
  • jQuery, adhering to "usual"/"good" jQuery practices.
  • AngularJS, adhering to "usual"/"good" AngularJS practices.

And you can then compare the process you went through to achieve each of the above, as well as the final code you wrote for each.

Michal Charemza
  • 25,940
  • 14
  • 98
  • 165
3

You can compare Javascript and Jquery because jQuery is a library which minimizes the complex javascript code into the simple methods and properties. You can explain jquery in a single line Write less do more but not AngularJS Framework.

DOM Manipulation is a powerful feature which is given by jQuery. You can manipulate DOM elements change the styling of a page. Play a Hide-and-Seek withe DOM elements, and many more. There is no templating like feature, all you need to code your application according to you requirement. For example, if you want to bind a keyup event to a textbox and wanted to display the changed value in a span then you need to write code like,

$('input').on('keyup',function(){
    $('span').text(this.value);
)};

On the other end Data Binding is a powerful feature which can be accomplished by AngularJs. This type of framework are commonly used for SPA(Single Page Applications), where data binding is must for example, online test. The advantage of using the Framework is that you will get the template including the framework. So you can bind the elements together by using ng- module like,

<input type='text' ng-model='inputElem'>
<span ng-bind='inputElem'></span>

And, I think the comparison can be,

  1. Javascript and Jquery
  2. Jquery and Angular (Not Javascript and Angular)

One more thing here, Javascript is a language, Jquery is a library and Angular is a Framework.

Below are the links which will clear more about library and framworks,

  1. What is the difference between a JavaScript framework and a library?
  2. What does AngularJS do better than jQuery?
  3. jQuery vs. AngularJS: A Comparison and Migration Walkthrough
Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106