[Go to site: main page, start]

0% found this document useful (0 votes)
22 views3 pages

JavaScript Basics: Key Concepts Explained

The document explains various JavaScript concepts including tokens, the prompt() syntax, assignment operators, and the difference between shift() and push(). It also covers function definition, the 'with' clause for accessing object properties, and the Date object for handling current date and time. Each concept is accompanied by examples for better understanding.

Uploaded by

gx59368
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

JavaScript Basics: Key Concepts Explained

The document explains various JavaScript concepts including tokens, the prompt() syntax, assignment operators, and the difference between shift() and push(). It also covers function definition, the 'with' clause for accessing object properties, and the Date object for handling current date and time. Each concept is accompanied by examples for better understanding.

Uploaded by

gx59368
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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. [Link]:

- document: HTML ka main object.

- bgColor: Property jo webpage ka background color set karne ke liye hoti hai.

Example: [Link] = "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];

[Link](); // arr becomes [2, 3]

[Link](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() {

[Link]("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);

[Link](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();

[Link](today); // Current date and time

[Link]([Link]()); // Current year

Common questions

Powered by AI

The prompt() method's strength lies in its simplicity and directness, providing a quick way to capture user input during script execution. It's useful for rapid prototyping and testing. However, its weaknesses include a lack of customization and modern styling, which can result in an inconsistent user experience across different browsers. Additionally, its modal nature can interrupt the user's workflow and should be avoided in sophisticated user interfaces .

Defining functions in JavaScript greatly enhances code reusability by allowing code snippets to be easily reused across different parts of an application. Functions encapsulate behavior, reducing duplication and improving readability. This modular approach simplifies maintenance by localizing changes to function definitions rather than throughout the codebase. As applications grow in complexity, functions facilitate organized and maintainable code, aiding in long-term project scalability and debugging .

The prompt() method is used to display a dialog box which prompts the user for input, typically used in situations where input from the user is needed during the execution of a script. It accepts two parameters: a message that appears as instructions for the user, and an optional default input value. When a default value is provided, it's pre-filled in the input field, which can be useful for guiding user responses or suggesting a typical input .

Assignment operators in JavaScript are used to assign values to variables, keeping expressions concise and easier to read. They range from simple assignment with '=' to compound assignments like '+=', which adds to and reassigns the variable in a single operation. For example, a = 10 assigns 10 to 'a', while a += 5 increments 'a' by 5, resulting in 'a' having a value of 15. These operators help in performing arithmetic updates efficiently within minimal lines of code .

The shift() method removes the first element from an array, resulting in the array elements being shifted left by one index, while the push() method adds a new element to the end of an array, increasing the array's length by one. For example, starting with an array var arr = [1, 2, 3], using arr.shift() changes it to [2, 3], and then arr.push(4) changes it to [2, 3, 4].

The document.bgColor property is used to set or retrieve the background color of an HTML document. It's a quick way to change background colors directly through JavaScript. However, its use is generally discouraged in favor of CSS due to better separation of concerns (style vs. behavior) and more powerful styling capabilities. CSS allows for more control, like using gradients or images, which bgColor cannot achieve .

Increment operators, such as '++', are used to increase the value of a variable by one. When used as a prefix, as in ++a, the operator increments the value of 'a' prior to its use in any expression, which means 'a' becomes 6 if it initially equals 5. This operation is particularly useful for loops and iteration where variables need to be incremented efficiently .

Functions in JavaScript are defined using the function keyword followed by a name and a block of code encapsulated within curly braces. For example, function greet() { console.log('Hello!'); } creates a function named greet. Functions enable code reusability, improve modularity, and streamline debugging by encapsulating specific tasks. By calling functions, repetitive code can be centralized and maintained easily .

The 'with' clause simplifies the process of accessing properties of a specified object by reducing the repetitive need to reference the object multiple times. It sets the scope to that object, allowing properties to be accessed as if they were variables in local scope. For example, using with (Math) {var result = sqrt(16) + pow(2, 3);} simplifies access to Math.sqrt and Math.pow, resulting in a calculation of result = 12 .

A JavaScript Date object can be used to represent both the current date and time, and offers various methods to extract specific components such as the year. To fetch the current year, the getFullYear() method is used, which returns the year of the specified date according to local time. For instance: var today = new Date(); var currentYear = today.getFullYear(); this retrieves the current year from the Date object .

You might also like