Jabarkan Contoh mudah dari JavaScript dalam bahasa melayu

if (n === 0) {

return n * factorial(n - 1);

Sintaks fungsi awanama (atau lambda):

function add (i, j) {

var add_pri = function (x, y) {

Demonstrasi fungsi variadik. 1 akan dialert, kemudian 2, seterusnya 3. arguments merupakan pemboleh ubah khas.

for (var i = 0; i < arguments.length; i++) {

Kod sampel ini menunjukkan pelbagai tampilan-tampilan JavaScript.

/* Finds the lowest common multiple of two numbers */

function LCMCalculator (x, y) { // constructor function

var checkInt = function (x) { // inner function

if (x % 1 !== 0) {

throw new TypeError(x + "is not an integer"); // exception throwing

// ^ semicolons are optional (but beware since this may cause consecutive lines to be

//erroneously treated as a single statement)

// The prototype of object instances created by a constructor is

// that constructor's "prototype" property.

LCMCalculator.prototype = { // object literal

constructor : LCMCalculator, // when reassigning a prototype, set the constructor property appropriately

gcd : function () { // method that calculates the greatest common divisor

var a = Math.abs(this.a), b = Math.abs(this.b), t;

if (a < b) {

while (b !== 0) {

b = a % b;

// Only need to calculate gcd once, so "redefine" this method.

// (Actually not redefinition - it's defined on the instance itself,

// so that this.gcd refers to this "redefinition" instead of LCMCalculator.prototype.gcd.)

// Also, 'gcd' == "gcd", this['gcd'] == this.gcd

this['gcd'] = function () {

"lcm" /* can use strings here */: function () {

// Variable names don't collide with object properties, e.g. |lcm| is not |this.lcm|.

// not using |this.a * this.b| to avoid FP precision issues

var lcm = this.a / this.gcd() * this.b;

// Only need to calculate lcm once, so "redefine" this method.

this.lcm = function () {

toString : function () {

return "LCMCalculator: a = " + this.a + ", b = " + this.b;

// Note: Array's map() and forEach() are predefined in JavaScript 1.6.

// They are currently not available in the JScript engine built into

// Microsoft Internet Explorer, but are implemented in Firefox, Chrome, etc.

// They are used here to demonstrate JavaScript's inherent functional nature.

[[25, 55],[21, 56],[22, 58],[28, 56]].map(function (pair) { // array literal + mapping function

}).sort(function (a, b) { // sort with this comparative function

/* Note: print() is a JS builtin function available in Mozilla's js CLI;

* It is functionally equivalent to Java's System.out.println().

* Within a web browser, print() is a very different function

* (opens the "Print Page" dialog),

* so use something like document.write() or alert() instead.

// print       (obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm());

// alert       (obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm());

document.write(obj + ", gcd = " + obj.gcd() + ", lcm = " + obj.lcm() + "<br />");

Keluaran berikut sepatutnya akan dipaparkan pada tetingkap pelayar.

LCMCalculator: a = 28, b = 56, gcd = 28, lcm = 56Jika Internet Explorer digunakan, contoh tadi akan menghasilkan ralat. Dengan itu, contoh tadi menunjukkan bahawa pentafsir JScript bagi Internet Explorer melakukan kod dengan cara berbeza berbanding pentafsir JavaScript dan ECMAScript dalam pelayar web lain. (Lihat ulasan dalam kod sumber untuk perincian perbezaan yang relevan bagi contoh ini.)