Javascript Assigment 1
Javascript Assigment 1
Javascript Assigment 1
Single-line commments start with"//"and extend to the end of the line.They are used
to add short explanations or notes within the code.For example:
Multi-line comments starts with"/"and end with"/".They can span across multiple
lines and are often used for longer explanations or for temporarily disabling
code.For example:
/*
This is a multi-line comment in JavaScript.
It can span across multiple lines.
*/
It's important to note that commments are ignored by the ole interpreter and do
not affect the execution of the code.However,they play a significantstand role in
improving code quality and maintainability.
Here are some best practies for writing comments in JavaScript:
* Be Clear and concise:Write comments that are easy to underpplement and provide
clear explanation of the code's purpose.
* Use Comments Sparingly:Avoid over-commenting your code.Comments should supplement
the code,not duplicate it.
* Use Comments to Explain Complex Logic:If you have complex or non-obvious logic in
your code,use comments to explain it to other developers(and your future self!).
* Comment as You Code:Get into the habit of adding comments as you write your code
rather than trying to add them all later.This ensure that the comments accurately
reflect the code's
intent.
* Upate Comments Regulaly:As you make changes to your code,remember to update the
corresponding comments to keep them accurate and relevant.
* Follow Consistent Style:Choose a commenting style (e.g.,placement)and stions and
stick to it throughout your codebase for consistency.
* Document Function and APls:INclude comments for function declarations and APls to
describe their purpose,parameters,return values,and any side effects.
By following best practices,you can use comments to improve the
reability,maintainability,and overall quality of your Javascript codebase.
Remember,good comments can save you and your fellow a lot of time and headaches in
the long run!
* Function Expression:
const functionName = function(parameters) {
// code block
};
* arror Function:
const functionName = (parameters) => {
// code block
};
* Function Parameters:
function greet(name) {
console.log('Hello, ${name}!');
}
greet('John');
* Return Statement:
function add(a, b) {
return a + b;
}
const sum = add(3, 5);
console.log(sum); // Output: 8
* Default Parameters:
function great(name = 'Guest')
console.log('Hello, ${name}!');
}
great(); // Output: Hello, Guest!
* Function Invocation:
functionName(arguments);
* Nested Functions:
function outerFunction() {
function innerFunction() {
// code block
}
innerFunction();
}
outerFunction();
* Scope:
. Functions have their own scope.
Variables declared within a function are not accessible from outside.
* Callback Functions:
function doSomething(callback) {
// code block
callback();
}
function greet() {
console.log('Hello!');
}
doSomething(great); //Output: Hello!
* Function Hoisting:
. Function declarations are hoisted to the top of the scope,allowing you to call
function before they are defined.
* Method Functions:
. Functions defined as properties of objects are called methods.
const obj = {
method() {
// code block
}
};
*Constructor Functions:
. Used to create objects with their own properties and methods.
These are the basic concepts and techniques for creating function in JavaScript.
Understanding the nuances of each data type is crucial for waiting efficient and
bug-free JavaScript code. By levrraging the appropriate data types and
understanding their behaviours,developers can create robust and maintainable
applications.
4. Difference between"=="and"==="?
ans) The operators"=="and"==="are used for comparison in programming languages,with
each having its own significance depending on the language being used.
In many programming languages,JavaScript,"=="is the equality operator,used to
compare the values of two variables.it checks if the values of two variables. it
checks if the values are equal,regardless of their data types.For example,in
JavaScript,5=="5"would return true because both values are equal in value,even
though one is a number and the other is a string.
On the other hand,"==="is known as the strict equality operator.It not only
compares the values of two variables but also checkes if their data types are the
same.In JavaScript,5==="5" would return false because because even though the
values are equal,their data types are different(number and string).
In this case,the first comparison(x == y) reture true because the values are equal,
while the second comparison (x===y)returns false bacause the data types are
different.
(a)for loop: The 'for' loop repeats a block of code a specified number of
limites.It consists of three optional expression:initialization,condition,and
iteration.
for (initialization; condition; iteration) {
// code to be executed
}
(b)while loop:The 'while' loop repeates a block of code while a specified condition
is true.
while (condition) {
// code to be executed
}
(a) Explicit Assignment:You can explicitly assign the value NULL to a variable to
indicate that it doesn't have a meaningful value.
let foo = NULL;
(d) Checking for Null:To check if a value is NULL,you can use a strict equality
comparison.
if (variable == NULL) {
// Do something
}
(f) Traversing the DOM:If you can't directly access an element, you can traverse
the DOM using methods like paraentsNogde,nextSibling, previousSibling,etc.
(g) Data Attributes:You can use data attributes to select elements.This method is
flexible and allows you to select elements based on custom attributed.For example:
var element = document,querySelector('[data-id="123"]');
(b) Logical OR(||):This operator returns true if at least one of the operands
is true; otherwise,it return false.For example,'true || false'evaluates to
true,while'false || false' evalutes to false.
(c) Logical NOT(!):This operator negates the boolean value of its operand.If
the operand is true,it returns true.For example,'!true'evaluates to false,while '!
false'evaluates to true.
(d) Eqality(==):This operator compares two values and returns true if they are
equal,regardless of their data types.For example,'5 == 5'evalutes to true, while
'5'also evalute to true due to type coercion.
(e) Strict Equality(===): this operator compares two values and returns true
if they are equal and of the same data type.For example,'5 == 5'evaluates to false
because they are of different data types.
(g) Strict Inequality(!==):This operator returns true if the operands are not
equal or not of the same data type.For example,'5 !== '5'evalutes to true because
they are of different data types.