Skip to main content

Posts

Showing posts with the label object

Know Everything about Objects in Javascript - Basics

As we know in Javascript there are two different datatypes - Primitive Datatype like string, number, boolean, undefined, null, Symbol(introduced in ES6) and Reference Datatype like object.
Primitive datatypes are immutable(cannot be changed) and values contain only a single thing (a string or a number etc) whereas Objects are mutable(can be changed).
What is Object? An object is an unordered list of data types, stored as name-value(or key-value) pairs. Each item in the list is called a property (and functions are called methods).e.g.
let person = {name : "Alex",  age: 27 };
console.log(person); 
output:
{name: "Alex", age: 27}age: 27name: "Alex"__proto__: Object We can also create the same person object differentlylet person = new Object();
person.name = "Alex";
person.age = 27;
console.log(person);
output:
{name: "Alex", age: 27} Generally, property names are kept as string but it can be a string or a number.e.g.
let person = { name : "A…

How to Spread in Javascript - ES 6

Spread syntax was introduced as part of Javascript in the ES2015/ECMAS-6.

This allows iterable likes array, object or string to get expanded.

Spread can be used in following ways:
in array : [...iterableArray, 3 , 'four', 5]
in object : {...iterableObj}

The spread operator is very useful for concat, copying,apply, expanding, math operations,etc in array and objects. Let discuss each in detail.

Concat
var arr1 = [1,2,3];
var arr2 = [4,5,6];

without spread
var concatenatedArray = arr1.concat(arr2); // [1,2,3,4,5,6]

with spread
var concatenatedArray = [...arr1, ...arr2 ] // [1,2,3,4,5,6]

Copying
var arr1 = [1,2,3];
var arr2 = [...arr1];
arr2.push(4);
console.log(arr2); // [1,2,3,4]
console.log(arr1); // [1,2,3]

Replacement for apply
var add = (a,b,c) => a+b+c;
var arr1 = [1,2,3];
add(...arr1); // replacing add.apply(null,arr1);

Expanding array
var programming = ["Java", "NodeJs", "Python"];
var subjects = ["Physics", ...programmi…

Javascript Interview Question and Answer - 1

In this article we are going to discuss 2 javascript interview question and their answers.These questions are highly asked in the Javascript interviews.

Disclaimer: - You might have different solution working with all test cases for both the problems.

So lets discuss the first question.This question is to test your ability in terms javascript object knowledge and your logical thinking.

Q1. You have been given a nested object "Person" which has one of the property as "password".Write a generic function to change the value of all the "password" property to "*****". Object is nested and may contain other object with property "password" and so on. Here is a sample object.

var Person = {
     name: 'Alex',
     age : 40,
    password : 'abc',
    x : {
       name : 'Chris',
       password: 'abcd',
       y : {
          name : 'Mike',
          password: 'abcd',
         },
     }
.
.
}

Before we …