# JavaScript/Syntax examples

These are simple examples of JavaScript syntax.
Variables in JavaScript can be defined using either the var, let or const keywords. Variables defined without keywords will be defined at the global scope.
Note the comments in the examples above, all of which were preceded with two forward slashes.
There is no built-in input/output functionality in JavaScript, instead it is provided by the run-time environment. The ECMAScript specification in edition 5.1 mentions that "there are no provisions in this specification for input of external data or output of computed results". However, most runtime environments have a console object that can be used to print output. Here is a minimalist "Hello, World!" program in JavaScript in a runtime environment with a console object:
In HTML documents, a program like this is required for an output:
A simple recursive function to calculate the factorial of a natural number:
An anonymous function (or lambda):
This example shows that, in JavaScript, function closures capture their non-local variables by reference.
Arrow functions were first introduced in 6th Edition – ECMAScript 2015. They shorten the syntax for writing functions in JavaScript. Arrow functions are anonymous, so a variable is needed to refer to them in order to invoke them after their creation, unless surrounded by parenthesis and executed immediately.
Example of arrow function:
In JavaScript, objects can be created as instances of a class.
Object class example:
In JavaScript, objects can be instantiated directly from a function.
Object functional example:
Variadic function demonstration (arguments is a special variable):
Immediately-invoked function expressions are often used to create closures. Closures allow gathering properties and methods in a namespace and making some of them private:
Generator objects (in the form of generator functions) provide a function which can be called, exited, and re-entered while maintaining internal context (statefulness).
JavaScript can export and import from modules:
Export example:
Import example:

### More advanced example

This sample code displays various JavaScript features.
The following output should be displayed in the browser window.

## References

* ↑ "var – JavaScript". The Mozilla Developer Network. Archived from the original on December 23, 2012. Retrieved December 22, 2012.
* ↑ "let". MDN web docs. Mozilla. Archived from the original on May 28, 2019. Retrieved June 27, 2018.
* ↑ "const". MDN web docs. Mozilla. Archived from the original on June 28, 2018. Retrieved June 27, 2018.
* ↑ "ECMAScript Language Specification – ECMA-262 Edition 5.1". Ecma International. Archived from the original on November 26, 2012. Retrieved December 22, 2012.
* ↑ "console". Mozilla Developer Network. Mozilla. Archived from the original on February 28, 2013. Retrieved April 6, 2013.
* ↑ "arguments". Mozilla Developer Network. Mozilla. Archived from the original on April 13, 2013. Retrieved April 6, 2013.
* ↑ "function* - JavaScript | MDN". developer.mozilla.org. Retrieved 2022-09-27.
* ↑ "JavaScript modules". MDN Web Docs. Mozilla. Archived from the original on 17 July 2022. Retrieved 28 July 2022.