JavaScript 2-Marks Questions Explained
1(a): Tokens in JavaScript
1. ++a:
- ++: Increment operator, jo ek value ko 1 se badhata hai.
- a: Variable, jisme value store hoti hai.
Example: Agar a = 5, toh ++a karne ke baad a = 6.
2. document.bgColor:
- document: HTML ka main object.
- bgColor: Property jo webpage ka background color set karne ke liye hoti hai.
Example: document.bgColor = "red"; se background red ho jayega.
1(b): Syntax of prompt()
- prompt() ek dialog box dikhata hai jisme user input de sakta hai.
Syntax:
prompt("Message", "Default Value");
- Message: Text jo user ko dikhai dega.
- Default Value: Optional hai, jo default input ke liye hai.
Example:
var name = prompt("What is your name?", "Guest");
1(c): Assignment Operators
- Assignment operators variables ko values assign karte hain.
- =: Assign value. Example: a = 10;
- +=: Add and assign. Example: a += 5; (means a = a + 5).
Example:
var a = 10;
a += 5; // a becomes 15
1(d): shift() vs push()
- shift(): Pehle element ko remove karta hai.
- push(): Naye element ko end mein add karta hai.
Example:
var arr = [1, 2, 3];
arr.shift(); // arr becomes [2, 3]
arr.push(4); // arr becomes [2, 3, 4]
1(e): Defining a Function
- Ek function ek block of code hai jo specific task karta hai.
Syntax:
function functionName() {
// code
Example:
function greet() {
console.log("Hello!");
greet(); // Output: Hello!
1(f): Small 'with' Clause
- with clause ek object ke multiple properties ko access karna easy banata hai.
Syntax:
with (object) {
// code
Example:
with (Math) {
var result = sqrt(16) + pow(2, 3);
console.log(result); // Output: 12
1(g): Date Object
- Date object current date aur time ko handle karne ke liye hota hai.
Example:
var today = new Date();
console.log(today); // Current date and time
console.log(today.getFullYear()); // Current year