DEV Community

ana
ana

Posted on • Updated on

Array Or Object In JavaScript language.

Image description

Array or Object It is a very important topic in JS.
If you want to learn javaScript in a website, youTube, book or All facilities.. there is a subject with this name => Array and object.
And something else that should be here, is difference between these two => Array and Object.
in first we should know what is Array! and then what is Object!!

before I explain, I want to tell you some resources for learn JS.

  1. https://www.w3schools.com/js/default.asp

  2. https://overapi.com/javascript

3.https://www.javascript.com/

but you should know "Books" is better than every choice for learn JS.

like "javasSript from beginner to professional" || "you don't know javaScript"

( yup, I use || for Or;) if you know..).

syntax of a Array:

Image description

There are three points here:

  1. I use [] for write a Array.
  2. I use any name I like for Variable.
  3. I can use from var or let instead const.(but there is some rules for that, and usually we use const for write a Array.)

if you don't know what is var or let or maybe const, check here:

https://www.w3schools.com/js/js_variables.asp

syntax of Object:

Image description

and Object Points..:

  1. I use{} for write a Object.
  2. I use a variable for every item in Object.
  3. name, age, nationality its a "key" for "value" I wrote in front of it.

well, in end remember that:
name, age, etc.. its a "key"
ana, 19, etc.. its a "value"
and we say that =>
name is a key for ana and ana its a value.
=> We say in the term (key-value).

syntax for array:

const arrayName = [value1, value2,...]
Enter fullscreen mode Exit fullscreen mode

syntax for object:

const objectName = {key1 : value1, ...}
Enter fullscreen mode Exit fullscreen mode

The difference between Array And Object:

now we should let's answer to that question:
what is difference between a Array and a object?

as you can see: I wrote array with [ ] and then I wrote directly value on it.

and then I wrote object with { } and write value but after key of that value.

but the main difference between a Array and Object its index of these two.

"index" its really easy and important subject and rule for array and object in JS.

in a Array every value from first have a index and that index start with 0, and then 1 and then 2 and...n. you should know we can't change that rule for Array. but in object:

in a Object every value from first have index but that index is what we call, for example for "ana" I give "name" for index of this value.
for another value I give index I want, after that in our program we wrote and we have Object
every value in Object have index and that index its "key" we wrote for value.

summery:

index of array start with 0,1,2,...:

const array1 = ["a", "b"]
Enter fullscreen mode Exit fullscreen mode

=> index of "a" its: 0
=> index of "b" its: 1

index of object start with key1, key2,..:

const object1 = {name: "ana", age:"19"}
Enter fullscreen mode Exit fullscreen mode

=> index of "ana" its name
=> index of "19" its age

forgive me if I forget semicolon :)

The end.

Top comments (1)

Collapse
 
anateotfw profile image
ana

my first writing in here ;)
enjoyyy.