Sitemap

Java Script Qs

4 min readMay 1, 2024

--

console.log('5'+3): Here, the + operator is used with a string ('5') and a number (3). In JavaScript, when you use the + operator with a string and a number, the number gets implicitly converted into a string and then concatenated with the other string. So, '5'+3 evaluates to the string '53'.

console.log('5'-3): Here, the - operator is used with a string ('5') and a number (3). In this case, JavaScript tries to perform a mathematical operation (subtraction). Since the string '5' cannot be subtracted from a number directly, JavaScript tries to convert the string into a number. The string '5' can be converted into the number 5. So, '5'-3 evaluates to the number 2.

console.log('5' == 5) outputs true because the string '5' is converted to the number 5 before comparison, and both operands are of the same type and have the same value.

console.log('5' === 5) outputs false because the strict equality operator === compares the string '5' with the number 5 without performing any type conversion, and they are of different types, hence unequal.

NaN stands for "Not-a-Number" in JavaScript, and it represents a value that is not a valid number. When you use the typeof operator with NaN, it returns 'number'.

This might seem counterintuitive, but it’s because NaN is considered a numeric data type in JavaScript. It's a special value representing the result of an invalid mathematical operation, such as dividing zero by zero or attempting to convert a string to a number where the string does not represent a valid number.

So, console.log(typeof NaN) will output 'number', indicating that NaN is of type number.

console.log(typeof NaN); // Output: 'number'

In this example, we're using the typeof operator to check the type of NaN. Despite NaN standing for "Not-a-Number," JavaScript considers it a numeric data type. Therefore, typeof NaN returns 'number'.

Here's

console.log(NaN === NaN); // Output: false

exmple :
+"12" is number because string 12 can be converted into integer but,
+"Hi" is NaN
+"Hello" is also NaN

That's why ,
console.log(NaN === NaN); // Output: false
console.log(+"Hi" === +"Hello") // Output: false

Because NaN can be come from non simillar value.

This might seem odd at first glance. NaN represents "Not-a-Number," so shouldn't it be equal to itself? However, due to its nature, NaN is not equal to any value, including itself. This is specified by the IEEE 754 floating-point standard, which JavaScript adheres to.

console.log(1 / 0); // Output: Infinity
console.log(-1 / 0); // Output: -Infinity

These examples illustrate how certain operations result in Infinity or -Infinity, which are also considered numeric values.

Now, consider:

console.log(typeof Infinity); // Output: 'number'
console.log(typeof -Infinity); // Output: 'number'

Both Infinity and -Infinity are considered numeric values in JavaScript, so their types are 'number'.

So, in summary, typeof NaN returning 'number' might seem counterintuitive, but it's consistent with JavaScript's treatment of NaN as a numeric value in the language's specification.

These expressions might seem straightforward, but they can lead to unexpected results due to how JavaScript handles comparison operators.

console.log(1 < 2 < 3):

This expression evaluates as follows:

1 < 2 evaluates to true because 1 is less than 2.

Get Vinayak Sannaik’s stories in your inbox

Join Medium for free to get updates from this writer.

Now, we have true < 3.

In JavaScript, true is implicitly converted to 1, so this becomes 1 < 3.

Finally, 1 < 3 evaluates to true.

Therefore, console.log(1 < 2 < 3) outputs true.

console.log(3 > 2 > 1):

This expression evaluates as follows:

3 > 2 evaluates to true because 3 is greater than 2.

Now, we have true > 1.

In JavaScript, true is implicitly converted to 1, so this becomes 1 > 1.

Finally, 1 > 1 evaluates to false.

Therefore, console.log(3 > 2 > 1) outputs false.

HTML
<h1>Click Me </h1>

JAVASCRIPT
const arr = ["vin", "sun", "bun"];
let index = 0; // Declare index outside the function

let clickBtn = document.querySelector('#click')

clickBtn.onclick = function() {
handleNameChange(arr);
}

function handleNameChange(arr){
clickBtn.innerHTML = arr[index];
index = (index + 1) % arr.length; // Update index for the next click
}

Async and defer are attributes that can be used with the <script> tag in HTML to control how scripts are loaded and executed.

  1. Async:
  • When you include the async attribute in a <script> tag, it indicates to the browser that the script can be downloaded asynchronously. This means that the script will not block the parsing of the HTML document. Instead, it will be downloaded in parallel with the HTML parsing process. Once the script is downloaded, it will be executed asynchronously, regardless of the order in which it was defined in the HTML document.
  • Example: <script src="script.js" async></script>

2. Defer:

  • When you include the defer attribute in a <script> tag, it indicates to the browser that the script should be executed after the HTML document has been fully parsed. This means that the script will not block the rendering of the page. It will be downloaded asynchronously (similar to async), but it will be executed in the order in which it appears in the HTML document, after the document has been parsed.
  • Example: <script src="script.js" defer></script>

In summary:

  • Both async and defer allow scripts to be loaded asynchronously, improving the page loading performance.
  • The key difference between them is the timing of script execution:
  • async scripts are executed as soon as they are downloaded, regardless of HTML parsing.
  • defer scripts are executed after HTML parsing is complete, in the order they appear in the document.

--

--

Vinayak Sannaik
Vinayak Sannaik

Written by Vinayak Sannaik

Dedicated dev crafting solutions for everyday challenges, delivering seamless user experiences. Passionate about problem solving ,innovation,continuous growth.