I’m very new to JavaScript, and have recently learned about declaring variables with var
. like var a = 12
or whatever. But, I came across a line in some code for a website I was reading through for fun which read var t={};
.
This was actually only the second line of code.
I can’t seem to find any explanation online anywhere for what it means to set a variable to equal a set of empty curly braces.
I thought it might be a way of declaring an array or something??
This defines a variable as an empty object
var t = {}
This defines it as an empty array
var t = []
This defines it as a boolean
var t = true
This defines it as an empty string
var t = ''
This defines it as an integer(number)
var t = 0
These are the basic building blocks/data types of javascript. There are plenty of tutorials online which cover this in the first lesson.
https://javascript.info/object
https://javascript.info/types
An object is a collection of data, it can contain arrays, booleans, strings, integers and even other objects. Objects consist of key/value pairs:
var user = {
// the key here is name and the value is a string 'Tom'
name: 'Tom',
// the value can also be an integer
age: 23,
// or an array
interests: ['gaming','travel','guitar'],
// or a boolean
loggednIn: false,
// or a nested object
contact: {
email: '[email protected]',
number: 01296714100
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With