let
const
let x = 10; //Some JS statements x = 20;
const COLUMNS = 80; // ... COLUMNS = 120; // Uncaught TypeError: Assignment to constant variable.
var
_
$
&
%
myVar
myvar
form
element
backgroundColor
userForm
mainElement
bgColor
lowerCamelCase
CONSTANT_CASE
UpperCamelCase
let x; // Declare a variable x x = 10; // Assign 10 to x
let x = 10; // Declare and assign x x = x + 10; // Add 10 to x x = x * 5; // Multiply x by 5 x = x / 5; // Divide x by 5 // Alternative shorthand x += 10; // Equivalent to x = x + 10 x *= 5; // Equivalent to x = x * 5 x /= 5; // Equivalent to x = x / 5
let x = 10; // x is a number x = "Hello"; // Now x is a string
'text'
"text"
true
false
+
-
*
/
++
--
+=
-=
*=
/=
%=
let x = 10, y = 3; console.log(x + y); // โ 13 (Addition) console.log(x - y); // โ 7 (Subtraction) console.log(x * y); // โ 30 (Multiplication) console.log(x / y); // โ 3.33 (Division) console.log(x % y); // โ 1 (Remainder) console.log(x ** y); // โ 1000 (Exponentiation) x++; console.log(x); // โ 11 (Increment) y--; console.log(y); // โ 2 (Decrement)
10 % 3
12 % 4
x += y
x *= y
let firstName = "Michael"; let lastName = " Scott"; let fullName = firstName + lastName; console.log(fullName); // "Michael Scott" let age = 21; let message = "I am " + age + " years old."; console.log(message); // "I am 21 years old." // Mixing numbers and strings console.log("10" + 5); // "105" (5 is converted to string) console.log(10 + 5); // 15 (Both are numbers)
parseInt("42"); // Converts a string to a number alert("Hello!"); // Displays a popup
console.log("Hello!"); // Method of the console object document.getElementById("id"); // Finds an element in the DOM document.write("Hello!"); // Writes to the document
function functionName(parameter(s)) { // Function body (instructions) // ... return result; // Optional }
return
function greet() { console.log("Hello, welcome to JavaScript!"); } greet(); // Output: "Hello, welcome to JavaScript!"
function add(a, b) { return a + b; } let sum = add(5, 3); console.log(sum); // Output: 8
sum
ex14.html
script
sitemap.html
ex14-2.html