diff --git a/Question1.js b/Question1.js new file mode 100644 index 0000000..cc04a7a --- /dev/null +++ b/Question1.js @@ -0,0 +1,26 @@ +/* +Q1.What is the difference between ++i and i++? + */ + +/* +The ++i pre-increment operator first increments the value then returns the value, +whereas in i++ post-increment operator it first returns current value then increments it. + */ + +//++i +let i = 0; +console.log(++i); //1 + +//++i can also be describe as follow: +let preIncrememtValue = 0; +preIncrememtValue = preIncrememtValue + 1; +console.log("Working of ++i", preIncrememtValue); //1 + +// i++ +i = 0; +console.log(i++); //0 + +//i++ can also be describe as follow: +let postIncrementValue = 0; +console.log("Working of i++", postIncrementValue); //0 +postIncrementValue = postIncrementValue + 1; diff --git a/Question2.js b/Question2.js new file mode 100644 index 0000000..303f8e9 --- /dev/null +++ b/Question2.js @@ -0,0 +1,28 @@ +/* +Q2.What do you think would happen if you pass an index beyond the range +of the string? Or if you pass a negative index? Try it out. +*/ + +let str = "hello"; + +console.log("String Length", str.length); + +/* + Accessing char that is out of range i.e. char after 5 lets say 6 +*/ + +console.log("Accessing char by string array", str[5]); +//array act as a object if we pass index out of range, it returns undefined + +console.log("Accessing char out of range", str.charAt(6)); +//returns empty string('') if try to access the character that is not in range + +/* +Passing negative index to charAt string method +*/ + +console.log("Accessing char by string array", str[-1]); +//array act as a object if we pass index as negative, it returns undefined + +console.log("Passing negative index", str.charAt(-1)); +//returns empty string('') if try to access the character by giving negqative index diff --git a/Question3.js b/Question3.js new file mode 100644 index 0000000..9f46b66 --- /dev/null +++ b/Question3.js @@ -0,0 +1,34 @@ +/* +Q3.Do you think JSON.stringify would work for arrays as well? + What about nested objects? + What happens if we pass numbers, strings, undefined, null to JSON.stringify? + */ + +//JSON.stringfy() +//It is a method where it takes object as parameter, +//and returns string (object in stringfied format). + +let arr = ["apple", "mango", "papaya"]; +console.log(typeof arr); +const fruits = JSON.stringify(arr); +console.log("Stringified Array : ", fruits); + +console.log("---------------------------"); + +let animal = { + kingdom: { domestic: "local", wild: "santuary" }, + isExtinct: false, +}; + +const strAnimal = JSON.stringify(animal); +console.log("Nested Object Stringified : ", strAnimal); + +console.log("---------JSON.stringify() operations-------"); + +console.log("JSON.stringify(number) =", JSON.stringify(10)); + +console.log("JSON.stringify(string) =", JSON.stringify("hello")); + +console.log("JSON.stringify(null) =", JSON.stringify(null)); + +console.log("JSON.stringify(undefined) =", JSON.stringify(undefined)); diff --git a/Question4.js b/Question4.js new file mode 100644 index 0000000..1b3c5bd --- /dev/null +++ b/Question4.js @@ -0,0 +1,28 @@ +/* +What happens if you pass a regular/invalid JSON string to JSON.parse? +What will happen if such an invalid function runs in the program? + Will other parts of the code execute correctly after that? +*/ + +const changeStringToObject = (str) => { + let object = JSON.parse(str); + + console.log("Successfully converted"); + return object; +}; + +let validString = '{"name":"amar","age":26}'; +let invalidString = '{"name":"amar";"age":26}'; //used semi-colon instead of comma + +console.log(changeStringToObject(validString)); +console.log(changeStringToObject(invalidString)); + +/* +JSON.parse() is a method where we pass JSON string as parameter +and we get object (object-parsed from string) in return. + +If we pass invalid JSON string, it throws syntax error. + +Since JS is interpreted language, in function if the JSON.parse method throws error +it will not execute the rest of the code. + */