Reputation: 1
I have created an object named computer but it is giving me the error Uncaught SyntaxError: Invalid shorthand property initializer and Uncaught ReferenceError: computer is not defined
What I can be done where I am going wrong? Please help.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript">
computer = {drive: "floppy", cpu = "intel", ram: "ddr"}
</script>
</head>
<body>
<script type="text/javascript">
document.write(computer.drive);
</script>
</body>
</html>
Upvotes: -1
Views: 70
Reputation: 9329
You have two errors:
These sound scary but they are both quite straight forward to fix.
Invalid shorthand property initializer
{drive: "floppy", cpu = "intel", ram: "ddr"}
^
This =
should be a :
like the other bits of your object. If you'd have Googled this on its own you would have been able to fix it yourself. The shorthand syntax is something else you can use, so if you wrote something like:
var cpu = "intel";
var computer = {drive: "floppy", cpu, ram: "ddr"}
That would be an example of valid shorthand, and is the same as writing
var cpu = "intel";
var computer = {drive: "floppy", cpu: cpu, ram: "ddr"}
Uncaught ReferenceError: computer is not defined
When we write var
or let
or const
we are declaring variables. In your case, you just write:
computer = { ...
The error is telling you quite clearly that you haven't defined a variable called that, or it can't find one. This is a simple fix:
var computer = {
As a sidenote, notice that above when declaring a variable it is correct to use an equals sign, but when you are in an {}
object, you should always use colons.
Upvotes: 0
Reputation: 2587
You forgot to declare it.
const computer = {drive: "floppy", cpu: "intel", ram: "ddr"}
Upvotes: 0