1.1
Basis of JS
Examples with a Run button are editable. Underlined terms open a short definition.
Basis of JS
- On page Script
<script type="text/javascript">
...
</script>
- Include external JS file
<script src="filename.js"></script>
- Output
console.log(a); // write to the browser console
document.write(a); // write to the HTML
alert(a); // output in an alert box
confirm('Really?'); // yes/no dialog, returns true/false depending on user click
prompt('Your age?', '0'); // input dialog. Second argument is the initial value
- Comments
/* Multi line
comment */
// One line
- Strict mode
'use strict'; // Use strict mode to write secure code
x = 1; // Throws an error because variable is not declaredExplore the idea
previous was assigned score’s primitive value before the reassignment.
score = 10previous = 10
Illustrative model. The controls step through the behavior described above.Your last opened chapter is saved in this browser when local storage is available.
Discuss this section
Questions, corrections or a better example? Comments use GitHub Discussions.