Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of var t={}; Variable equals curly braces

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??

like image 368
rgolden Avatar asked Sep 13 '25 04:09

rgolden


1 Answers

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
  }
}
like image 138
Pixelomo Avatar answered Sep 15 '25 17:09

Pixelomo