Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

JS Objects

There is the phrase, “In JavaScript, objects are king. If you understand objects, you understand JavaScript.”.

In JavaScript, almost “everything” is an object. All JavaScript values, except primitives, are objects. Booleans/Numbers/Strings can be objects (if defined with the new keyword). Dates/Maths/Arrays/Functions are always objects.

JavaScript Primitives: A primitive value is a value that has no properties or methods. Primitive values are immutable (they are hardcoded and therefore cannot be changed). A primitive data type is a data that has a primitive value. E.g., if num = 5, you can change the value of num, but you cannot change the value of 5.

JavaScript has 5 types of primitive data types: string, number, boolean, null, undefined.

As we know, in JavaScript, variables can contain single values. Objects are variables, too, but objects can contain many values. We write them as name : value pairs (name and value separated by a colon).

Example:   
var Stud = {firstName:"Adarsh", lastName:"Chaudhary", age:21, eyeColor:"black"}; 

So we can say that A JavaScript object is a collection of named values. The named values in JavaScript objects are called properties.

The first way to create an Object using literal we saw in the above example. An object can also be created using a new keyword.

Example:   
var Stud = new Object();  
Stud.firstName = "Adarsh";  
Stud.lastName = "Chaudhary";  
Stud.age = 21;  
Stud.eyeColor = "black";

We may access JS Object properties in two ways:

  • objectName.property
  • objectName["property"]

New FOR loop {for (variable in object)}:

We have seen a normal for loop in the previous tutorial of the loop. Here we will see a new way of writing for loops to iterate through the properties of objects. Let us try to understand that through example.

var Stud = {firstName:"Adarsh", lastName:"Chaudhary", age:21, eyeColor:"black"};   
var temp = "";  
for (var x in Stud) {  
 temp += Stud[x] + " ";  
}  
So in temp **Adarsh Chaudhary 21 black** will be stored.

Try the following example in the editor below.

You are given an Object mentioning marks of a student in three subjects. Please note 1st subject is Maths, 2nd is Physics and 3rd is Chemistry. All subject’s marks are distinct. Print the name of the Subject(Either Maths or Physics or Chemistry) having the highest marks. (You are not allowed to change the template.)

Sample Input:

Object_Stud = {20, 40, 30}

Sample Output:

Physics

Start solving JS Objects on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test