JavaScript is the world's most popular programming language. Show
Table of Contents
JavaScript is the programming language of the Web. JavaScript is easy to learn. This tutorial will teach you JavaScript from basic to advanced. Start learning JavaScript now » Examples in Each ChapterWith our "Try it Yourself" editor, you can edit the source code and view the result. Use the MenuWe recommend reading this tutorial, in the sequence listed in the menu. If you have a large screen, the menu will always be present on the left. If you have a small screen, open the menu by clicking the top menu sign ☰. Learn by ExamplesExamples are better than 1000 words. Examples are often easier to understand than text explanations. This tutorial supplements all explanations with clarifying "Try it Yourself" examples. If you try all the examples, you will learn a lot about JavaScript, in a very short time! JavaScript Examples » Why Study JavaScript?JavaScript is one of the 3 languages all web developers must learn: 1. HTML to define the content of web pages 2. CSS to specify the layout of web pages 3. JavaScript to program the behavior of web pages This tutorial covers every version of JavaScript:
Learning SpeedIn this tutorial, the learning speed is your choice. Everything is up to you. If you are struggling, take a break, or re-read the material. Always make sure you understand all the "Try-it-Yourself" examples. The only way to become a clever programmer is to: Practice. Practice. Practice. Code. Code. Code ! Commonly Asked Questions
You don't have to get or download JavaScript. JavaScript is already running in your browser on your computer, on your tablet, and on your smart-phone. JavaScript is free to use for everyone. My LearningTrack your progress with the free "My Learning" program here at W3Schools. Log into your account, and start earning points! This is an optional feature. You can study W3Schools without using My Learning. JavaScript ReferencesW3Schools maintains a complete JavaScript reference, including all HTML and browser objects. The reference contains examples for all properties, methods and events, and is continuously updated according to the latest web standards. Complete JavaScript Reference » JavaScript Quiz TestTest your JavaScript skills at W3Schools! Start JavaScript Quiz! Kickstart your careerGet certified by completing the course Get certified w3schoolsCERTIFIED.2022A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" invokes it (calls it). Example // Function to compute the product of p1 and p2 Try it Yourself » JavaScript Function SyntaxA JavaScript function is defined with the Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: The code to be executed, by the function, is placed inside curly brackets: {} function name(parameter1, parameter2, parameter3) { Function parameters are listed inside the parentheses () in the function definition. Function arguments are the values received by the function when it is invoked. Inside the function, the arguments (the parameters) behave as local variables. Function InvocationThe code inside the function will execute when "something" invokes (calls) the function:
You will learn a lot more about function invocation later in this tutorial. Function ReturnWhen JavaScript reaches a If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. Functions often compute a return value. The return value is "returned" back to the "caller": ExampleCalculate the product of two numbers, and return the result: let x = myFunction(4, 3); // Function is called, return value will end up in x function myFunction(a, b) { The result in x will be:
Try it Yourself » Why Functions?You can reuse code: Define the code once, and use it many times. You can use the same code many times with different arguments, to produce different results. ExampleConvert Fahrenheit to Celsius: function toCelsius(fahrenheit) { Try it Yourself » The () Operator Invokes the FunctionUsing the example above, Accessing a function without () will return the function object instead of the function result. Example function toCelsius(fahrenheit) { Try it Yourself » Functions Used as Variable ValuesFunctions can be used the same way as you use variables, in all types of formulas, assignments, and calculations. ExampleInstead of using a variable to store the return value of a function: let x = toCelsius(77); You can use the function directly, as a variable value: let text = "The temperature is " + toCelsius(77) + " Celsius"; Try it Yourself » You will learn a lot more about functions later in this tutorial. Local VariablesVariables declared within a JavaScript function, become LOCAL to the function. Local variables can only be accessed from within the function. Example// code here can NOT use carName function myFunction() { // code here can NOT use carName Try it Yourself » Since local variables are only recognized inside their functions, variables with the same name can be used in different functions. Local variables are created when a function starts, and deleted when the function is completed. Test Yourself With ExercisesExercise:Execute the function named function myFunction() { alert("Hello World!"); } ; Start the Exercise Foreach di JavaScript untuk apa?Perulangan Foreach di Javascript Perulangan foreach biasanya digunakan untuk mencetak item di dalam array. Perulangan ini termasuk dalam perulangan counted loop, karena jumlah perulangannya akan dituentukan oleh panjang dari array. Apa itu for in JavaScript?Perulangan FOR IN adakah perulangan khusus di dalam JavaScript yang digunakan untuk menampilkan 'isi' dari sebuah objek. Objek yang digunakan dapat berupa Array, karena array pada dasarnya juga merupakan objek di dalam JavaScript. Apa itu toFixed JavaScript?Method JavaScript: Number.toFixed() Method toFixed digunakan untuk membuat tampilan angka dengan jumlah desimal yang tetap. Method ini membutuhkan satu parameter opsional yang jika diinput akan menentukan jumlah digit setelah tanda desimal. Apa itu Map di JavaScript?Map adalah sebuah koleksi dari elemen-elemen yang ada pada JavaScript yang tiap elemen ini disimpan sebagai pasangan KV (key, value). Hal ini memungkinkan untuk objek pada JavaScript Map untuk untuk menahan objek dan nilai primitif sebagai kunci atau nilainya. |