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.
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:
There are three points here:
- I use
[]
for write a Array.- I use any name I like for Variable.
- 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:
syntax of Object:
and Object Points..:
- I use
{}
for write a Object.- I use a variable for every item in Object.
- 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,...]
syntax for object:
const objectName = {key1 : value1, ...}
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"]
=> index of "a" its: 0
=> index of "b" its: 1
index of object start with key1, key2,..:
const object1 = {name: "ana", age:"19"}
=> index of "ana" its name
=> index of "19" its age
forgive me if I forget semicolon :)
The end.
Top comments (1)
my first writing in here ;)
enjoyyy.