I'm curious as to why this particular scenario is occurring:
var obj = { 0:"bla", 1:"blabla" }
For some reason, when I try to access obj.0
, an error is returned and I can only use obj[0]
. Why is that the case?
I'm curious as to why this particular scenario is occurring:
var obj = { 0:"bla", 1:"blabla" }
For some reason, when I try to access obj.0
, an error is returned and I can only use obj[0]
. Why is that the case?
An acceptable naming identifier cannot begin with a number. It's a simple rule to follow. To access properties, you must use the dot notation if the keys are valid identifiers. Otherwise, you need to utilize square bracket notation like this: object['0']
object[0]
is the same as object['0']
because property keys must be strings (any string can act as a key), so the value inside the brackets is automatically converted to a string.
Your code is encountering a SyntaxError
because of a missing "
after bla
:
var obj = { 0:"bla", 1:"blabla" }
The reason why obj.0
results in a SyntaxError
is because JavaScript recognizes property accesses using the syntax:
<identifier>.<identifier>
In JavaScript, identifiers cannot start with numbers. This is why trying to declare var 1a = 1;
will lead to a SyntaxError
due to an unexpected number.
Instead, by using obj[0]
, JavaScript interprets 0
as a string and accesses properties based on keys (which are strings) in associative arrays like this:
obj["constructor"]
For more information on identifier syntax in JavaScript, refer to section 7.6 of the ECMAScript Language Specification.
The excerpt below outlines the syntax for identifiers (extracted from the provided link):
IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart
IdentifierStart ::
UnicodeLetter
$
_
\ UnicodeEscapeSequence
let object = { 0:'hello', 1:'world' } //incorrect
The string 'hello' is not enclosed in double quotes properly, so the right way to do it is:
let object = { 0:"hello", 1:"world" } //correct
Give this a shot:
set up
let data = { "0":"hello", "1":"world" };
retrieve
alert(data[0]);
alert(data[1]);
Hey there! I've been working on a form that sends a value to a new window, but for some reason, the value val2 is showing up as null in the new window instead of being passed. Here's my code: function sendValue(value) { ViewImg = window.ope ...
When an array int arr[5] is initialized but more than 5 elements are stored in it, the extra element gets memory allocated in a separate space. In the past, older compilers like Turbo would report crashes due to something getting overwritten in memory. H ...
I'm currently working with some code that involves callbacks: function getUserToken(data, callback) { var password_sha256 = sha256(data.password); getAppById(data.app_id).then(function(res) { console.log("app"+res); if (!r ...
Currently, I'm working on a website using Bootstrap and I am incorporating both light and dark themes. Everything seems to be functioning well except for one issue - when the page is refreshed, the browser doesn't retain the theme that was previo ...
I recently incorporated jquery Galleria into my website and I am currently facing an alignment issue with the div element gallery-container. Interestingly, it appears correctly aligned in Chrome but is shifted to the right in Firefox. You can view the webs ...
{ "entries": [ { "id": 23931763, "url": "http://www.dailymile.com/entries/23931763", "at": "2013-07-15T21:05:39Z", "message": "I ran 3 miles and walked 2 miles today.", "comments": [], "likes": [], ...
I am looking to add a button to the AppBar component in Material UI that, when clicked, will redirect me to a new page (/login from /). I have successfully used history.push() to achieve this functionality. However, the new page does not display the expect ...
After updating data with an ajax function and refreshing the div, I encountered an issue where the jQuery elements inside the div break. How can this be resolved? function getURLParameter(name) { return decodeURIComponent((new RegExp('[?|&]&apo ...
As I attempt to link my client-side JavaScript with a backend websocket service utilizing socket.io, I encounter an issue. I am attempting to establish a connection to the socket.io server using the native WebSocket object: new WebSocket("wss://localhost ...
I am facing an issue with a large mesh containing over 5 million triangles. I utilized BufferGeometry with attributes such as position, color, normal, and index. However, there comes a point where I need to remove certain indices from the index attribute. ...
I am new to three.js and apologize if my question is a bit complex. I have set up my scene in three.js but now I want the camera's position to smoothly transition from point A to point B (which I will specify in my code). I plan on using Tween.js to ...
Can anyone assist me in troubleshooting why my array_search function is not working properly within this PHP script I've written? Despite moving the array_search around and trying different values, I still can't seem to locate the issue. I have p ...
While searching for a tutorial on integrating REST APIs with Firebase, I came across numerous code snippets utilizing the curl method calls or other helper libraries. However, what I couldn't find were the basics - such as where to call these methods ...
After importing a Mesh element into three.js from an fbx file, I am looking to enhance it with an effect similar to that of a clickable button. I am aiming to achieve the same pushable button effect as demonstrated in the first button style found on http ...
Can Vuex state be used to access class methods? For example, in this scenario, I am attempting to invoke fullName() to show the user's formatted name. TypeError: store.state.user.fullName is not a function Classes export class User { constructo ...
I am facing an issue with rendering html elements on a canvas using html2canvas JS. I am utilizing AngularJS for data binding on the html page, but unfortunately, the dynamic data is not showing up on the canvas generated from these html elements. For inst ...
I want to implement a feature where a div is hidden when its child element is empty. To achieve this, I aim to assign the class .no-content to the div if it contains no elements. Below is my existing code with spaces: <div class="ee-posts-list&quo ...
I am hoping to utilize gulp for the following tasks: Compiling TypeScript to JavaScript, which is easily achievable Concatenating JavaScript files in a specific order, proving to be challenging Since I am developing an Angular application, it is crucial ...
Is there a more efficient way to manipulate array A to get array B, considering the given variable "k"? I have come up with the following logic but believe there may be a better approach. The length of the array is always divisible by k This part is no ...
Is it possible to generate HTML and SVG code based on the position of a Subscriber on a web page or within a specific div? I would like to provide some JavaScript code that can be inserted inside a particular div on the page. Using an iframe is not ideal ...