How JS Converts Expressions Into Boolean?
JavaScript’s ToBoolean operation converts any expression into a boolean.
Why is conversion required?
Because the while(expr) loop runs only when
ToBoolean(expr) === true.
JavaScript’s falsy values
There are only a handful of falsy values:
-
falseboolean -
0number -
-0negative zero -
0nBigInt zero -
""empty string -
nullnull -
undefinedundefined -
NaNNot-a-Number
Note: Everything else—objects, arrays, functions, non-empty strings, and non-zero numbers—is truthy.
slice - js
In JavaScript, slice() is used to extract a portion of an array or string without modifying the original. It works on arrays and strings. It does not modifies original.
array.slice(start, end); string.slice(start,end)
JavaScript Data Types and How They Are Stored
Understanding the difference between primitive values and objects in memory.
| Type | Example | What variable stores? |
|---|---|---|
| Primitive (string, number, boolean, null, undefined, bigint, symbol) | "har", 10, true |
The actual value itself |
| Object (array, function, object) | {}, [], function(){} |
A reference (pointer) to the object |
In JavaScript, any non promitive data type is a kind of object hence ,arrays are a special kind of object, which is why -
[1,3] is // "object"
In JavaScript, the for...of loop is used to iterate over the values of an iterable object, such as an array, string, Map, or Set. It gives you direct access to each element’s value, making it ideal when you only care about the data itself. On the other hand, the for...in loop is used to iterate over the keys (or property names) of an object. It’s commonly used for looping through object properties rather than array elements. While for...in works with objects and returns the keys as strings, for...of works with iterables and returns their actual values. Therefore, for...of is better suited for arrays and other iterable structures, while for...in is typically used for plain JavaScript objects.
const arr = ["a", "b", "c"];
for (const x of arr) console.log(x); // a, b, c
for (const x in arr) console.log(x); // 0, 1, 2
// in → key , use of in should be avoided for arrays
// of → value
array and operations on array - js
const fruits = ["apple", "banana", "mango"];
console.log(fruits.includes("banana")); // true
console.log(fruits.includes("ban")); // false , full element should match
console.log(fruits.shift()); // Removes the first element of an array, and returns that element
console.log(fruits.unshift()); // Adds new elements to the beginning of an array, and returns the new length
console.log(fruits.push("orange")); // push() → Add element at the END
console.log(fruits.pop()); // Returns removed element -> "orange"
"hello world".split() // No separator is provided ,The entire string becomes one single array element. // ["hello world"]
"hello world".split("") // Empty string "" is used as the separator,The string is split character by character // ["h","e","l","l","o"," ","w","o","r","l","d"]
["h", "e", "l", "l", "o"].join() // JavaScript uses comma , as the default separator . output -"h,e,l,l,o"
["h", "e", "l", "l", "o"].join("") // Elements are joined without anything in between . output - "hello"
// to reverse a string
let reversed = str.split("").reverse().join("");
spread and rest operator (...)
| Concept | Spread (...) |
Rest (...) |
|---|---|---|
| Purpose | Expand/expand elements of an iterable (array, object, string) into individual elements. | Collect multiple elements/arguments into a single array or collect remaining properties. |
| Typical syntax | [...arr], {...obj}, fn(...args) |
function f(...args), const { a, ...rest } = obj |
| Used in | Array literals, object literals, function calls, iterable expansions. | Function parameters, array/object destructuring to gather remaining items. |
| Result | Produces individual elements (does not create nested arrays unless element is nested). | Creates an array of the remaining arguments/items or an object with remaining props. |
| Mutates original? | No — creates new arrays/objects when used in literals or calls. | No — creates a new array/object holding collected values. |
| When to prefer | When you need to merge or expand values (e.g., shallow-copy arrays, pass array as args). | When you want to accept variable number of args or extract "rest" fields in destructuring. |
| Function | Direction | Converts | Example | Output | Type |
|---|---|---|---|---|---|
toString() |
Number → String | Converts any value to string | (123).toString() |
"123" |
string |
parseInt() |
String → Number | Converts string to integer | parseInt("123") |
123 |
number |
parseFloat() |
String → Number | Converts string to float | parseFloat("12.5") |
12.5 |
number |
we can also use js object as map.but objects only accept string or symbol keys — not other data types.
const map = new Map();
// Add key-value pairs
map.set('name', 'Harmeet');
map.set('age', 25);
map.set(true, 'Yes'); // can use boolean as key
// Get a value
console.log(map.get('name')); // "Harmeet"
// Check if key exists
console.log(map.has('age')); // true
// Delete a key
map.delete('age');
// Get map size
console.log(map.size); // 2
// Iterate through entries
for (const [key, value] of map) {
console.log(key, value);
}
The structuredClone() method in JavaScript is a built-in global function used to create a deep copy of an object or value.
string
string - js
// string matching js
const text = "JavaScript is fun";
console.log(text.includes("Script")); // true
console.log(text.includes("script")); // false (case-sensitive)
let s2 = 'World';
let s3 = `Hello ${s2}`; // template literal
// string matching python
string - pyton
text = "Python is awesome"
print("Python" in text) # ✅ True
print("python" in text) # ❌ False (case-sensitive)
js code snippets
To find quotient use
let quotient = Math.floor(sum / 10); // sum/10 would give a float value is it is not fully divisible
different naming conventions
naming conventions —
ways to write multi-word names (like variables, functions, classes) in code
Style
Example
Common Use Case
camelCase
userName
JS variables, functions, object keys
PascalCase
UserName
Classes, React components
snake_case
user_name
Python, databases, environment variables
kebab-case
user-name
CSS class names, HTML attributes