To write (log) something on the console
console.log("Hello World");
console.log(12345);
console.log(2 + 2);
console.log("Hello", "World", 123);<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hello JavaScript!!</h1>
<script src="app.js"></script> <!-- Linking JS File -->
</body>
</html>Used to add embedded expressions in a string.
let a = 5;
let b = 10;
console.log(`Price is ${a + b} rupees`);
// console.log("Price is", a + b, "rupees");- Arithmetic (+, -, *, /, %, **)
- Unary (++, --)
- Assignment (=, +=, -=, *=, /=, %=, etc)
- Comparison
- Logical
Comparison Operators used to compare 2 values
>>=<<===!===,!=compares value, not type===,!==compares value & type
Logical operators are used to combine boolean expressions.
&&Logical AND||Logical OR!Logical NOT
true && true // true
true && false // false
false && true // false
false && false // false
true || true // true
true || false // true
false || true // true
false || false // false
!true // false
!false // trueEverything in JS is true or false (in boolean context).
This doesn't mean their value itself is true or false, but they are treated as true or false if taken in boolean context.
false, 0, -0, 0n (bigint value), ""(empty string), null, undefined, NaN
Everything else
- if
- if-else
- else-if
- switch
- Ternary Operator (? :)
if (some condition) {
// Do SOMETHING
}if (condition1) {
// Do SOMETHING
} else {
// Do SOMETHING ELSE
}if (condition1) {
// Do SOMETHING
} else if (condition2) {
// Do SOMETHING ELSE
}Nesting is writing if-else inside if-else statements. It can have many levels
if (condition1) {
if (condition2) {
// Do SOMETHING
} else {
// Do SOMETHING ELSE
}
} else {
// Do SOMETHING ELSE
}Used when we have some fixed values that we need to compare to.
let color = "yellow";
switch (color) {
case "red" :
console.log ("Stop");
break;
case "yellow" :
console.log ("Slow Down");
break;
case "green" :
console.log ("Go");
break;
default :
console.log ("Broken Light");
}-
Alerts displays an alert message on the page.
alert("Something is wrong!");
-
prompt displays a dialog box that asks user for some input.
prompt("Please enter name")