JavaScript can be added to an HTML file using the script tag:
<script src="script.js"></script>
Single-line comments in JavaScript start with //
, while multi-line comments start with **/*
**and end with */
.
// This is a single-line comment
/*
This is a multi-line comment
*/
You can print to the console in JavaScript using the console.log()
function:
console.log("Hello, world!");
Variables in JavaScript can be declared using let
, const
, or var
.
let x = 5; // a variable declared with let can be reassigned
const y = 10; // a variable declared with const cannot be reassigned
var z = 15; // a variable declared with var has function scope
Variables in JavaScript can be declared and assigned in the same line:
let x = 5;
JavaScript supports arithmetic operators such as addition, subtraction, multiplication, division, and modulus:
let x = 5;
let y = 10;
let z = x + y; // addition
let a = x - y; // subtraction
let b = x * y; // multiplication
let c = y / x; // division
let d = y % x; // modulus
Unary operators are operators that work with only one operand. JavaScript supports several unary operators, such as increment, decrement, and negation: