Welcome to the MDoanh Nghiệp beginner"s JavaScript course! In thisarticle we will look at JavaScript from a high level, answering questions such as "What is it?" and "What can you vì with it?", & making sure you are comfortable with JavaScript"s purpose.

Bạn đang xem: Player là gì

Prerequisites: Objective:
Basic computer literacy, a basic understanding of HTML & CSS.
To gain familiarity with what JavaScript is, what it can bởi, & how it fits into lớn a website site.

JavaScript is a scripting or programming language that allows you to lớn implement complex features on website pages — every time a website page does more than just sit there and display static information for you lớn look at — displaying timely nội dung updates, interactive sầu maps, animated 2D/3 chiều graphics, scrolling video clip jukeboxes, etc. — you can bet that JavaScript is probably involved. It is the third layer of the layer cake of standard website technologies, two of which (HTML và CSS) we have sầu covered in much more detail in other parts of the Learning Area.


The three layers build on top of one another nicely. Let"s take a simple text label as an example. We can mark it up using HTML lớn give sầu it structure và purpose:

p>Player 1: Chrisp>


Then we can add some CSS inlớn the phối to lớn get it looking nice:

p font-family: "helvetica neue", helvetica, sans-serif; letter-spacing: 1px; text-transform: uppercase; text-align: center; border: 2px solid rgba(0,0,200,0.6); background: rgba(0,0,200,0.3); color: rgba(0,0,200,0.6); box-shadow: 1px 1px 2px rgba(0,0,200,0.4); border-radius: 10px; padding: 3px 10px; display: inline-block; cursor: pointer;


And finally, we can add some JavaScript lớn implement dynamic behavior:

const para = document.querySelector("p");para.addEventListener("click", updateName);function updateName() let name = prompt("Enter a new name"); para.textContent = "Player 1: " + name;

Try clicking on this last version of the text label to lớn see what happens (note also that you can find this test on GitHub — see the source code, or run it live)!

JavaScript can vị a lot more than that — let"s explore what in more detail.

So what can it really do?

The core client-sideJavaScript language consists of some common programming features that allow you lớn vì chưng things like:

Store useful values inside variables. In the above sầu example for instance, we ask for a new name khổng lồ be entered then store that name in a variable called name. Operations on pieces of text (known as "strings" in programming). In the above example we take the string "Player 1: " & join it lớn the name variable lớn create the complete text label, e.g. ""Player 1: Chris". And much more!

What is even more exciting however is the functionality built on top of the client-side JavaScript language. So-called Application Programming Interfaces (APIs) provide you with extra superpowers to use in your JavaScript code.

APIs are ready-made sets of code building blocks that allow a stamboom-boden.com khổng lồ implement programs that would otherwise be hard or impossible lớn implement. They vì chưng the same thing for programming that ready-made furniture kits vì for home building — it is much easier to take ready-cut panels and screw them together to lớn make a bookshelf than it is lớn work out the thiết kế yourself, go & find the correct wood, cut all the panels to lớn the right form size and shape, find the correct-sized screws, và then put them together lớn make a bookshelf.

They generally fall inkhổng lồ two categories.


Browser APIs are built into your web browser, and are able to lớn expose data from the surrounding computer environment, or do useful complex things. For example:

Note: Many of the above demos won"t work in an older browser — when experimenting, it"s a good idea khổng lồ use a modern browser like Firefox, Chrome, Edge or Opera to run your code in. You will need khổng lồ consider cross browser testing in more detail when you get closer to delivering production code (i.e. real code that real customers will use).

Third các buổi party APIs are not built into lớn the browser by default, and you generally have sầu to lớn grab their code và information from somewhere on the Web. For example:

Note: These APIs are advanced, and we"ll not be covering any of these in this module. You can find out much more about these in our Client-side website APIs module.

There"s a lot more available, too! However, don"t get over excited just yet. You won"t be able to build the next Facebook, Google Maps, or Instagram after studying JavaScript for 24 hours — there are a lot of basics to cover first. And that"s why you"re here — let"s move on!

What is JavaScript doing on your page?

Here we"ll actually start looking at some code, and while doing so, explore what actually happens when you run some JavaScript in your page.

Let"s briefly recap the story of what happens when you load a web page in a browser (first talked about in our How CSS works article). When you load a website page in your browser, you are running your code (the HTML, CSS, và JavaScript) inside an execution environment (the browser tab). This is like a factory that takes in raw materials (the code) & outputs a sản phẩm (the web page).


A very comtháng use of JavaScript is khổng lồ dynamically modify HTML & CSS khổng lồ update a user interface, via the Document Object Model API (as mentioned above). cảnh báo that the code in your web documents is generally loaded and executed in the order it appears on the page. Errors may occur if JavaScript is loaded and run before the HTML và CSS that it is intended lớn modify. You will learn ways around this later in the article, in the Script loading strategies section.

Xem thêm:

Browser security

Each browser tab has its own separate bucket for running code in (these buckets are called "execution environments" in technical terms) — this means that in most cases the code in each tab is run completely separately, and the code in one tab cannot directly affect the code in another tab — or on another trang web. This is a good security measure — if this were not the case, then pirates could start writing code lớn steal information from other websites, và other such bad things.

Note: There are ways lớn sover code & data between different websites/tabs in a safe manner, but these are advanced techniques that we won"t cover in this course.

JavaScript running order

When the browser encounters a block of JavaScript, it generally runs it in order, from top khổng lồ bottom. This means that you need lớn be careful what order you put things in. For example, let"s return lớn the bloông chồng of JavaScript we saw in our first example:

const para = document.querySelector("p");para.addEventListener("click", updateName);function updateName() let name = prompt("Enter a new name"); para.textContent = "Player 1: " + name;
Here we are selecting a text paragraph (line 1), then attaching an event listener lớn it (line 3) so that when the paragraph is clicked, the updateName() code bloông xã (lines 5–8) is run. The updateName() code bloông xã (these types of reusable code blocksare called "functions") asks the user for a new name, và then inserts that name into lớn the paragraph to lớn update the display.

If you swapped the order of the first two lines of code, it would no longer work — instead, you"d get an error returned in the browser stamboom-boden.com console — TypeError: para is undefined. This means that the para object does not exist yet, so we can"t add an sự kiện listener to it.

Note: This is a very common error — you need to be careful that the objects referenced in your code exist before you try lớn vị stuff khổng lồ them.

Interpreted versus compiled code

You might hear the terms interpreted and compiled in the context of programming. In interpreted languages, the code is run from top to bottom & the result of running the code is immediately returned. You don"t have sầu khổng lồ transkhung the code into a different size before the browser runs it. The code is received in its programmer-friendly text size và processed directly from that.

Compiled languages on the other hvà are transformed (compiled) inlớn another khung before they are run by the computer. For example, C/C++ are compiled inkhổng lồ machine code that is then run by the computer. The program is executed from a binary format, which was generated from the original program source code.

JavaScriptis a lightweightinterpreted programming language. The website browser receives the JavaScript code in its original text size and runs the script from that. From a technical standpoint, most modern JavaScript interpreters actually use a technique called just-in-time compiling lớn improve sầu performance; the JavaScript source code gets compiled into lớn a faster, binaryformat while the script is being used, so that it can be run as quickly as possible. However, JavaScript is still considered an interpreted language, since the compilation is handled at run time, rather than ahead of time.

There are advantages khổng lồ both types of language, but we won"t discuss them right now.

Server-side versus client-side code

You might also hear the terms server-side & client-side code, especially in the context of web development. Client-side code is code that is run on the user"s computer — when a website page is viewed, the page"s client-side code is downloaded, then run & displayed by the browser. In thismodule we are explicitly talking about client-side JavaScript.

Server-side code on the other hvà is run on the hệ thống, then its results are downloaded and displayed in the browser. Examples of popular server-side website languages include PHP, Pykhông lớn, Ruby,ASPhường.NET và... JavaScript! JavaScript can also be used as a server-side language, for example in the popular Node.js environment — you can find out more about server-side JavaScript in our Dynamic Websites – Server-side programming topic.

Dynamic versus static code

The word dynamic is used khổng lồ describe both client-side JavaScript, và server-side languages — it refers khổng lồ the ability khổng lồ update the display of a web page/ứng dụng to lớn show different things in different circumstances, generating new nội dung as required. Server-side code dynamically generates new content on the hệ thống, e.g. pulling data from a database, whereas client-side JavaScript dynamically generates new content inside the browser on the client, e.g. creating a new HTML table, filling it with data requested from the hệ thống, then displaying the table in a website page shown to lớn the user. The meaning is slightly different in the two contexts, but related, and both approaches (server-side và client-side) usually work together.

A website page with no dynamically updating content is referred to as static — it just shows the same nội dung all the time.

How bởi you add JavaScript to lớn your page?

JavaScript is applied to lớn your HTML page in a similar manner to CSS. Whereas CSS uses elements to lớn apply external stylesheets và elements to apply internal stylesheets to lớn HTML, JavaScript only needs one friover in the world of HTML — the
Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *