Skip to content

Commit ca41975

Browse files
authored
Question-Answer 35-36
1 parent 4b1f772 commit ca41975

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Top JavaScript interview questions
4040
| 32 | [What is strict mode in javaScript](#32-what-is-strict-mode-in-javascript) |
4141
| 33 | [What are the different ways to empty an array in javascript](#33-what-are-the-different-ways-to-empty-an-array-in-javascript) |
4242
| 34 | [What is NaN in javascript](#34-what-is-nan-in-javascript) |
43+
| 35 | [Javascript naming convention](#35-javascript-naming-convention) |
44+
| 36 | [Difference between call(), apply() and bind()](difference-between-call-apply-and-bind) |
45+
4346

4447
### 1. What is JavaScript
4548
* JavaScript is a scripting language used to create dynamic and interactive websites. It is supported by all major web browsers.
@@ -505,6 +508,69 @@ console.log("a"/1) // output ========> NaN
505508
console.log(Math.sqrt(-1)); // output ========> NaN
506509
console.log(parseInt("blabla")); // output ========> NaN
507510
```
511+
512+
### 35. Javascript naming convention
513+
1. Use camelCase (lowercase for the first word, then uppercase for subsequent words) for variable and function names
514+
2. Use PascalCase for class names (uppercase for the first letter of each word)
515+
3. Use "is" or "has" as prefixes for boolean variables
516+
4. Use UPPERCASE for constants
517+
5. Use descriptive and meaningful names for your variables, functions, and classes.
518+
519+
```js
520+
//variable
521+
let firstName = "Surbhi";
522+
523+
//function
524+
function displayName() {
525+
return "Surbhi Dighe";
526+
}
527+
displayName();
528+
529+
//boolean
530+
let isLoading = false;
531+
let hasName = true;
532+
533+
//constants
534+
let SECONDS = 60;
535+
536+
//class
537+
class DisplayName {
538+
constructor(firstName, lastName) {
539+
this.firstName = firstName;
540+
this.lastName = lastName;
541+
}
542+
}
543+
var name = new DisplayName('Surbhi', 'Dighe');
544+
```
545+
546+
### 36. Difference between call(), apply() and bind()
547+
call(), apply(), and bind() methods are used to attach a function into an object and call the function as if it belonged to that object.
548+
549+
**call()** - call() is used to invoke a function immediately with a specified *this* value and allows you to pass the arguments one by one
550+
```js
551+
let person = {name: 'Surbhi'};
552+
function printName(message) {
553+
console.log(message + ' ' + this.name); // output ========> "Hello Surbhi"
554+
}
555+
printName.call(person, 'Hello');
556+
```
557+
**apply()** - apply() is used to invoke a function immediately with a specified *this* value and allows you to pass the arguments as an array
558+
```js
559+
let person = {name: 'Surbhi'};
560+
function printName(message) {
561+
console.log(message + ' ' + this.name); // output ========> "Hello Surbhi"
562+
}
563+
printName.apply(person, ['Hello']);
564+
```
565+
**bind()** - bind() returns a new function (which can be invoked anytime), with a specified *this* value and allows you to pass in arguments
566+
```js
567+
let person = {name: 'Surbhi'};
568+
function printName(message) {
569+
console.log(message + ' ' + this.name); // output ========> "Hello Surbhi"
570+
}
571+
let sayHello = printName.bind(person, "Hello");
572+
sayHello();
573+
```
508574
******************************In progress
509575
510576

0 commit comments

Comments
 (0)