DEV Community

Cover image for Become a pro in JavaScript
Karthick (k)
Karthick (k)

Posted on

Become a pro in JavaScript

Q1.JavaScript console.log() Method.

In JavaScript, the console.log() method is a built-in JavaScript function that outputs messages to the console, which is a special area in web browsers or runtime environments for developers to view information about their code.

It is primarily used for

Debugging code
Inspecting variables
Tracking the flow of execution
Logging errors or warnings

console.log("");

Q2.Exponentiation ()**

The exponentiation (**) operator returns the result of raising the first operand to the power of the second operand. It is equivalent to Math.pow(), except it also accepts BigInts as operands.

Q3.Operator precedence

Operator precedence determines how operators are parsed with respect to each other. Operators with higher precedence become the operands of operators with lower precedence.

Q4. What are chaining assignments in JavaScript?

Chaining assignments in JavaScript is a syntax pattern that allows you to assign a single value to multiple variables in a single line of code.

let a, b, c;
a = b = c = 10;

Q5.Conditional (ternary) operator:

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy, followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as an alternative to an if...else statement.

`Syntax

(condition) ? expression1 : expression2
`
Q5.Scope of Variables in JavaScript

Global Scope

A global variable refers to a variable that is declared outside any function or block, so it can be used anywhere in the program, both inside functions and in the main code.

`// Global Variable accessed from within a function
const x = 10;

function fun1() {
console.log(x);
}

fun1();`

Local Scope

A local variable is a variable declared inside a function, making it accessible only within that function. It cannot be used outside the function.

Functions are objects and can be assigned to variables.

`function fun2(){

// This variable is local to fun2() and 
// cannot be accessed outside this function
let x = 10;
console.log(x);
Enter fullscreen mode Exit fullscreen mode

}

fun2();
`

Q6.The break Statement

The break statement is used to exit a loop when a certain condition is satisfied. It is commonly used when searching for a specific value in an array or when an early exit from a loop is required.

`Syntax

break;`

Q7.The Continue statement

The continue statement skips the current iteration of the loop and moves to the next iteration without terminating the loop.

`Syntax

continue;`

Q8 .How the switch Statement Works

In JavaScript, a *switch statement * is a control-flow mechanism that evaluates an expression and matches its value against multiple case clauses to execute specific code blocks.

`const fruit = "Apple";

switch (fruit) {
case "Banana":
console.log("Bananas are yellow.");
break;
case "Apple":
console.log("Apples are red or green.");
break;
default:
console.log("Unknown fruit.");
}
// Output: "Apples are red or green."
`
Key Rules for Using default

In JavaScript, the default clause in a switch statement acts as a fallback that executes when none of the specified case values matches the given expression. It serves the same purpose as the final else block in an if-else chain

`const expression = "orange";

switch (expression) {
case "apple":
console.log("Apples are $1.00");
break;
case "banana":
console.log("Bananas are $0.50");
break;
default: // Executes if expression is neither "apple" nor "banana"
console.log("Item not found");
break;

}`

Top comments (0)