Posts

Basic JavaScript Interview question and answers

Q 1 . What is JavaScript? A 1 . JavaScript is a scripting language used to create and control dynamic website content,  such as forms, animations, and interactive elements. Q 2 . What are the data types in JavaScript? A 2 . The data types in JavaScript include: strings, numbers, booleans,  objects, arrays, and functions. Q 3 . How do you define a variable in JavaScript? A 3 . Variables can be defined in JavaScript using the “var” keyword  followed by the variable name. Q 4 . What is the purpose of the “this” keyword in JavaScript? A 4 . The “this” keyword is used to refer to the object that the current  code is being executed in. Q 5 . What is the difference between == and === in JavaScript? A 5 . The “==” operator checks if two values are equal in value, while the “===”  operator checks if two values are equal in value and type. Q 6 . What is the purpose of the “undefined” keyword in JavaScript? A 6 . The “undefined” keyword is used to denote a variable that ha...

JavaScript Type Conversion Table

The below table shows the conversion of various values to String, Number, and Boolean in JavaScript.  Value String Conversion Number Conversion Boolean Conversion 1 "1" 1 true 0 "0" 0 false "1" "1" 1 true "0" "0" 0 true "ten" "ten" NaN true true "true" 1 true false "false" 0 false ...

JavaScript - Implict null to number conversion

// null is 0 when used with number let num; num = 4 + null; console.log(num); // 4 num = 4 - null; console.log(num); // 4

JavaScript Implicit Boolean Conversion to Number

In Boolean true is 1, false is 0 let num; num = '4' - true; console.log(num); // 3 num = 4 + true; console.log(num); // 5 num = 4 + false; console.log(num); // 4

JavaScript implict conversion to number

You can implict convert a string to number using the following operators - , / , * let num; num = '6' - '4'; console.log(num); // 2 num = '4' - 2; console.log(num); // 2 num = '4' * 2; console.log(num); // 8 num = '4' / 2; console.log(num); // 2

JavaScript Implict Conversion to String

Implicit conversion is when JavaScript automatically converts one data type to another. let str; str = '1' + 2; console.log(str) // "12" str = '1' + true; console.log(str); // "1true" str = '1' + undefined; console.log(str); // "1undefined" str = '1' + null; console.log(str); // "1null"

Swapping array elements in JavaScript

Elements can be swapped using destructuring assignment expression var numbers = [1,2,3]; [numbers[2], numbers[1]] = [numbers[1], numbers[2]]; console.log(numbers); // [1,3,2] Without destructuring assignment, swapping two values requires a temporary variable