Main content
Computer programming - JavaScript and the web
Course: Computer programming - JavaScript and the web > Unit 6
Lesson 7: Using JS libraries in your webpageWhat's a JS library?
There are a billion webpages, and many of them are interactive in some way - in fact, many of them are interactive in the same ways. How many websites have you seen that use a slideshow? I've seen hundreds, myself!
For example, here's one from National Geographic:
Here's one from Instagram:
One more, from The Onion:
Now, it's possible that every one of those web developers wrote their own JavaScript code to make a slideshow, but a lot of them probably re-used the same code. As programmers, we should re-use existing code when we can, so that we don't waste our time writing code that another programmer has already written.
In JavaScript, the way we do that is by using a library. A library is a JavaScript file that contains a bunch of functions, and those functions accomplish some useful task for your webpage.
How do we know what functions we can use? We could look at the JavaScript file, if it's short, or better, we could look at the documentation. Most libraries have documentation with a list of available functions or a real-world example.
For example, here's a snippet of the documentation for Galleria, a popular JS library for making slideshows:
When a programmer creates a JS library and puts it out into the world, they're purposefully deciding to share it with the world - so that often means they put in the effort to come up with great documentation and examples. Perhaps one day you will decide to create a library of some functionality that you find really useful and share it with the world.
But first, how do you actually use a library? I'll show you that next!
Want to join the conversation?
- Do you think Javascript should implant some librairies into the language directly instead of calling one when you code, if yes, then which ones ?(7 votes)
- There is no need to burden the language definition with functionality that can be easily implemented using the language.
Turning the question around, which ones might you suggest?
(FWIW, many people believe that the DOM is part of the Javascript language and assert that KA does not teach "real Javascript" because the DOM functions are not readily available while Procesing.js's functions are. They are mistaken.)(13 votes)
- Can i check the source of a website, then find the library the website using and open the .js file and copy it?
Is it copyright infringement?(7 votes)- Here's an answer found from google:
When you make any creative work including coding, the work is under exclusive copyright by default. Unless you include a license that specifies otherwise, nobody else can copy, distribute, or modify your work without being at risk of take-downs, shake-downs, or litigation.(1 vote)
- Is there any special "test" that your function has to go through to be included in the library? Or can anyone make a library that anyone can use?(5 votes)
- Okay, I'm not sure where to ask this (like always lol), but is there a function like
get()
in PJS that can be used in a website canvas? If not, is there a way I can turn a drawing into an image?(4 votes)- Yes, you can draw your own images.
https://www.khanacademy.org/computer-programming/images-52/1405420533(3 votes)
- The concept of libraries is something especific of JavaScript? Or is something comon in every program language?(3 votes)
- It is common for all languages. A library is just a collection of ready made functions or code.
There might be languages that cannot include the libraries, and so the code would need to be copied from the library into the project.(4 votes)
- In the JS tutorials on KA we use the processing JS library right So can we use the same library to make web pages? If so, can you please tell me how?(4 votes)
- Hello I am trying to learn javascript and I have got a book and also tried to learn at Khan Academy also but when I tried to use the draw method in the very first lesson it did not work for me. Could anyone help me because I am wanting to try and learn in the way I can create projects? I would appreciate it.(2 votes)
- Save your program. Then use its "Help Requests" tab to ask for assistance. That way we can see your program.(2 votes)
- What does slice do in java script I am fairly confused and the results seem to be random in slice and I am not sure(1 vote)
- Let's say i want to make an online game, how would i make an HTML replacement for rect(); or ellipse(); could anyone show me a tutorial for that if possible?(1 vote)
- there are a ton of js libraries. Is there anything common between all of them? which should i prefer?(1 vote)
- This seems to be addressed later in the course, so read on.(2 votes)