3 Keys Introduction To Objects Arrays
javaScript arrays

3 Keys Introduction To Objects Arrays


If you are a web developer, and I assume you are since you landed here reading this, you know that one of the most used programming languages is JavaScript.

The key to creating complex and efficient programs in JavaScript is the ability to work with objects and arrays.


3 Keys Introduction to Objects Arrays.

Arrays

An array is a collection of values, similar to a list or an array in other programming languages. You can think of an array as a box with many compartments, where each compartment holds a value. I wrote a more elaborate article about it?here. In JavaScript, you can create an array using square brackets and separate each value with a comma. For example, let’s say we want to create an array of letters:


let letters = [A, B, C, D];        

In this example, we’ve created an array called “letters” that contains four values. We can access each value in the array using its index, which is the position of the value in the array. Remember that in JavaScript, arrays are zero-indexed, which means the first value is at index 0, the second value is at index 1, and it goes on. For example, to access the first value in the “letters” array, we would use the following code:

console.log(letters[0]); // Output: A        

Objects.?Objects are collections of key-value pairs, also known as?properties. An object is like a dictionary, where each key represents a word and the value represents its definition. In JavaScript, you can create an object using curly braces { } and separating each key-value pair with a comma. For example, let’s say we want to create an object that represents a person:


let person = {
  name: 'Luc', //this is a string
  age: 45, //this is a number
  gender: 'male' //this is a string
};        

In this example, we’ve created an object called “person” that has three properties: “name”, “age”, and “gender”. We can access each property in the object using dot notation. For example, to access the “name” property, we would use the following code:


console.log(person.name); // Output: 'Luc'        

Arrays and objects can also be combined to create more complex data structures. For example, let’s say we want to create an array of users, where each user is an object with a “first” and “last” name:


let users = [
  { first: 'Luc', last: 'Constantin' },
  { first: 'John', last: 'Macintosh' },
];        

In this example, we’ve created an array called “users” that contains two objects, each representing a user. We can access the “first” property of the second user using the following code:


console.log(users[1].first); // Output: 'John'
//Arrays are zero-indexed, which means the first value starts at index 0(zero)
        

Console.table()

For an improved way to debug, we can use the?console.table()?method to display arrays and objects in a tabular format, which is useful for visualizing complex data structures. For example, to display the “users” array in a table, we would use the following code:


  let user = {
      first: 'Luc',
      last: 'Constantin',
      occupation: 'web developer',
      loggedin: true,
      hobbies: ['hiking', 'running', 'tennis']
        } 
     document.getElementById('title').innerText = user['first']
     console.table(user)        

the result in the console will be as seen in the below photo:

If you want to know more about?console.table()?see this article.

Let’s briefly recap:

  • arrays and objects are fundamental data structures in JavaScript
  • they are essential for creating complex and efficient programs
  • console.table() is great way?method to display arrays and objects in a tabular format

As a final thought I believe that by understanding how to work with arrays and objects, you can unlock the full potential of JavaScript and create amazing web applications, like shopping lists, to-do lists, and much more.


Is this helpful for you? Why not subscribe to the monthly newsletter and receive an email with all the articles I write about coding, and dev things, mainly?JavaScript?

Subscribe to Monthly Newsletter.


Hello Luc... We post 100's of job opportunities for developers daily here. Candidates can talk to HRs directly. Feel free to share it with your network. Visit this link - https://jobs.hulkhire.com And start applying.. Will be happy to address your concerns, if any

回复

要查看或添加评论,请登录

Luc Constantin的更多文章

  • 8 Keys Factors to improve SEO

    8 Keys Factors to improve SEO

    #seo #contentwriting #contentcreation #webdevelopment Article originally written on https://blog.accolades.

  • Career Timeline’s Power

    Career Timeline’s Power

    The Power of a Career Timeline: A Late Starter’s Guide to Success in the Tech Industry Article originally written on…

    1 条评论
  • 3 empowering Keys to enhance your Captcha Knowledge

    3 empowering Keys to enhance your Captcha Knowledge

    Have you wondered what CAPTCHA stands for? It stands for Completely Automated Public Turing test to tell Computers and…

  • 3 Keys in JavaScript Arrays

    3 Keys in JavaScript Arrays

    If you are new to programming and you are learning JavaScript, or if you already have met JavaScript you know the term…

  • Boost Your Profit with Google Local Business

    Boost Your Profit with Google Local Business

    As someone who aims to assist small businesses in enhancing their online visibility, I frequently suggest creating a…

  • Explain Strings in JavaScript like I am 6

    Explain Strings in JavaScript like I am 6

    As a newbie in the world of programming, learning a new language like JavaScript can be quite challenging. I titled…

  • Math ceil()in Javascript

    Math ceil()in Javascript

    Some time ago, I finished one of the Javascript projects from Chris Dixon’s series Math ceil()in Javascript is a very…

    1 条评论
  • I am a Slow Learner, is that bad?

    I am a Slow Learner, is that bad?

    Since I was a small kid I remember struggling to learn, so I guess that makes me a slow learner. The learning path for…

  • I am bad at coding, please help!

    I am bad at coding, please help!

    How many times you haven’t felt this way? How many times you’ve looked at your code and had the feeling of being the…

  • Start Your Freelance Journey in Four Easy Steps

    Start Your Freelance Journey in Four Easy Steps

    Freelance is scary, but it’s also freedom. Of course, there are already a plethora of articles, blogs, videos, and…

社区洞察

其他会员也浏览了