Clean β’ Professional
Answer: JavaScript is a high-level, interpreted programming language primarily used to add interactivity to web pages. It can run client-side in browsers or server-side via Node.js.
Key Features:
Example:
document.getElementById("myButton").addEventListener("click", () => alert("Clicked!"));
Answer:
<script> tags or external .js filesExample:
<script src="script.js"></script>
Answer:
<script> tags in <head> or <body><script src="file.js"></script><button onclick="myFunction()">Click</button>Example:
<script>
console.log("Hello, World!");
</script>
Answer:
console.log() β Console outputalert() β Popup messagedocument.write() β Writes directly to the document (not recommended for dynamic content)Example:
console.log("Log to console");
alert("This is an alert");
document.write("Written to document");
Answer:
{} for code blocks() for function calls and conditionsExample:
let name = "John";
if (name) {
console.log("Hello, " + name);
}
Answer: Instructions executed by the browser. Examples include variable declarations, loops, and conditionals.
Example:
let x = 5; // Declaration
x = x + 1; // Assignment
Answer: Comments explain code or prevent execution.
// Comment/* Comment */Example:
// Single-line comment
/* Multi-line
comment */
Answer:
var: Function-scoped, can be redeclared, hoistedlet: Block-scoped, can be updated, not redeclared in same scopeconst: Block-scoped, cannot be updated, must be initializedExample:
var a = 1;
let b = 2;
const c = 3;
Answer: Determines where a variable is accessible:
let and const limited to {}Example:
let globalVar = "I'm global";
function myFunc() {
let localVar = "I'm local";
console.log(globalVar); // Accessible
console.log(localVar); // Accessible
}
Answer: Moving declarations to the top of their scope. Only declarations, not initializations, are hoisted.
Example:
console.log(x); // undefined
var x = 5;
Answer: +, -, *, /, %, **, ++, --
Example:
let a = 10, b = 5;
console.log(a + b); // 15
console.log(a ** 2); // 100
Answer: ==, ===, !=, !==, >, <, >=, <=
Example:
console.log(5 == "5"); // true
console.log(5 === "5"); // false
Answer: Combine boolean expressions: &&, ||, !
Example:
let a = true, b = false;
console.log(a && b); // false
console.log(a || b); // true
Answer: String, Number, BigInt, Boolean, Null, Undefined, Symbol
Example:
let str = "Hello";
let num = 42;
let bool = true;
Answer:
null: Intentional absence of valueundefined: Variable declared but not assignedExample:
let x = null;
let y;
Answer: Returns the type of a variable as a string
Example:
console.log(typeof 42); // "number"
console.log(typeof null); // "object"
Answer: Changing a valueβs type, either explicitly or implicitly
Example:
let num = "123";
console.log(Number(num) + 1); // 124 (explicit)
console.log(num + 1); // "1231" (implicit)
Answer: Methods like toUpperCase(), toLowerCase(), slice(), trim(), replace()
Example:
let str = "Hello World";
console.log(str.toUpperCase()); // "HELLO WORLD"
console.log(str.slice(0, 5)); // "Hello"
Answer: toFixed(), toPrecision(), parseInt(), parseFloat()
Example:
let num = 3.14159;
console.log(num.toFixed(2)); // "3.14"
Answer: Provides constants and functions like Math.PI, Math.random(), Math.floor()
Example:
console.log(Math.random()); // Random between 0 and 1
console.log(Math.floor(3.7)); // 3
Answer: Represent true/false values, often used in conditionals
Example:
let isAdult = true;
if (isAdult) console.log("Eligible"); // "Eligible"
Answer: An ordered list of values indexed from 0
Example:
let arr = [1, 2, 3];
console.log(arr[0]); // 1
Answer: push(), pop(), shift(), unshift(), slice(), splice()
Example:
let arr = [1, 2];
arr.push(3); // [1,2,3]
console.log(arr.pop()); // 3
Answer: Executes a block of code if a condition is true, can use else/else if
Example:
let age = 18;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
Answer: Repeats a block of code a specified number of times
Example:
for (let i = 0; i < 3; i++) {
console.log(i); // 0, 1, 2
}
Answer:
break: exits loop entirelycontinue: skips current iterationExample:
for (let i = 0; i < 5; i++) {
if (i === 3) break;
console.log(i); // 0,1,2
}
Answer: Variables declared with let or const are limited to the {} block
Example:
{
let x = 10;
console.log(x); // 10
}
console.log(x); // ReferenceError
Answer: Named function using function keyword, hoisted to the top
Example:
function greet() {
return "Hello!";
}
console.log(greet()); // "Hello!"
Answer:
Example:
function add(a, b) { // parameters
return a + b;
}
console.log(add(2, 3)); // 2,3 are arguments
Answer: Collection of key-value pairs
Example:
let person = { name: "John", age: 30 };
console.log(person.name); // "John"Β