JS-1

Modified on Fri, 17 Feb, 2023 at 4:26 PM

When to use [ ] and .(dot) when dealing with objects? (ID: JS11)

The use of square brackets [ ] and .(dot) when dealing with objects in JavaScript depends on the type of object property you want to access.

For object properties that are just simple strings or symbols, you can use dot notation. For example:


For properties that have non-valid identifier names (such as spaces, numbers, or special characters), or if you want to access a property dynamically, you can use the square bracket notation:


It's also possible to use square bracket notation to access object properties that number.


How to use a slice or substr function and what is the difference between them? (ID: JS12)


In JavaScript, slice() and substr() are two string methods that allow you to extract a portion of a string.


The slice() method takes two arguments: start and end both optional. The start argument is the index of the first character to include in the new string, and the end argument is the index of the first character to exclude from the new string.


For example:


The substr() method also takes two arguments: start and length is optional. The start argument is the index of the first character to include in the new string, and the length argument is the number of characters to include in the new string.


For example:



So, in this case, the slice() and substr() methods return the same result. However, if you want to extract all the characters from a given index to the end of the string, you can use slice() without the end argument:

In contrast, if you want to extract all the characters from a given index to the end of the string using substr(), you would have to specify a very large length:

In conclusion, slice() is a more flexible method for extracting parts of a string and is recommended to use over substr() in most cases.


The major differences between them are:


What happens if we don't put var const or let when initializing a variable in JavaScript? (ID: JS13)


In JavaScript, if you don't use varconst, or let when initializing a variable, the variable will be created as a global variable, even if it's within a function.

This can lead to unexpected behavior, especially if multiple functions or scripts on the same page create global variables with the same name, as the last one to run will overwrite any previous values.

It's generally considered best practice to always use const or let when declaring variables in modern JavaScript, as this ensures that the variable is only available within the scope in which it's defined, and helps prevent naming collisions and other unintended consequences.

Here's an example of how not using var, const, or let can lead to unexpected behavior:

In this example, x is created as a global variable even though it's defined within the example function. This can lead to naming collisions and other unintended consequences and is generally considered bad practice.












What is the difference between var let and const? (ID: JS14)




What is the difference between == and === in javascript? I(ID: JS15)

The main difference between the == and === operators in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article