Jump to content

hisoka

Recommended Posts

when this script

 

<script>var p =(150 & 14) << 1;document.write(p);</script>

 

is executed , it gives 12 as ouput

 

now when I change the script to this

 

 

<script>var p = 10;

var p +=(150 & 14) << 1;document.write(p);</script>

 

it did not work . Why it did not work?

 

 

here in this link http://www.w3schools.com/js/js_operators.asp

 

it says :

 

+= x += y x = x + y

 

so normally it should give 22 as output

 

var p +=(150 & 14) << 1 is equal to var p = p+((150 & 14) << 1) is equal to 12 + 10 = 22

 

but as I wrote it did not work . why?

Link to comment
Share on other sites

Oh ###### , sometime I become blind and I write javascript in a notepad ( a normal notepad) and execute them without a debugger so that I cannot see when I make an error . After seeing that I made this error that , I normally , should not make . I downloaded , for some minutes , a javascript editor in the hope that it can debug these TRIVIAL errors that sometimes I cannot perceive . the name of the program is :

 

free javascript editor yaldex software version 4.7.2.7

 

but to my surprise when I click the debug function it does not debug and when I run this script in this javascript editor yaldex

 

<script>var p = 10 // no semi colonp +=(150 & 14) << 1 // no semi colondocument.write(p);</script>

 

it gives me 22 as output although there is no semi colon " ; " at the end of the first and second line in the script . Sometime when I click the run button this yaldex does not executed the script . It becomes like a magical thing :)

 

I do not know why ??? so why? Why the script was executed correctly although the script is wrong ??

 

do you know a good and an easy javascript editor with a debugger that I can download in my computer and use it so that it corrects these trivial errors ?? or could you tell me what' s going on with this yaldex software free editor?

Link to comment
Share on other sites

The errors you have are not really errors, you're just not writing the code that you think you are. It's perfectly fine to use var multiple times, that's not an error, but it might not do what you expect. In Javascript, semicolons at the ends of lines are usually optional, it's not an error to leave them out, but it's good practice to use them.I don't use a Javascript IDE, I just write the code and load it in my browser. The browser has Javascript debugging tools built in.

Link to comment
Share on other sites

it is a good idea to use the browser javascript debugging tools . But it make things more complicated for me :

 

 

 

<script>var p = 10;var p +=(150 & 14) << 1;document.write(p);</script>

when I clicked on firefox "tools" option then "web developer" then " web console" and put this script

 

<script>var p = 10;var p +=(150 & 14) << 1;document.write(p);</script>

 

Which is 100% correct , not only I got this error :"SyntaxError: expected expression, got '<" which I could not quite digest but it made me angry as my script is correct but it gives an error . So what servers me this option if it will make things complicated for me instead of facilitating things to me ?!

 

The debugger ans style editor and the rest in firefox are complicated to use and load millions of characters I mean they do not facilitate things to me .

 

Are there other options that I do not know?

 

or am I misusing the tools in firefox ?

 

may be you can tell me how toi benefit from these tools and show me what I am ignoring

Link to comment
Share on other sites

<script>var p = 10 // no semi colonp +=(150 &  14) << 1 // no semi colondocument.write(p);</script>

 

Ok, so you're playing with a bitwise AND operation and a bitwise shift and automatic semi-colon insertion. But what is the usefulness of this? Javascript uses IEEE-754 floating-point numbers so binary shifting is not an efficient technique. Also use of automatic semi-colon insertion is known to create buggy code.

Link to comment
Share on other sites

Which is 100% correct , not only I got this error :"SyntaxError: expected expression, got '<" which I could not quite digest but it made me angry as my script is correct but it gives an error .

It's because of the <script> tags. That is an HTML tag, not Javascript. Don't put HTML into the Javascript console.
  • Like 1
Link to comment
Share on other sites

"It's because of the <script> tags. That is an HTML tag, not Javascript. Don't put HTML into the Javascript console"

 

very good . I put the script without the <script> and </script> and I got 22 as output

 

However , when I provoked an error like this

 

var p = 10;var p +=(150 & 14) << 1;document.write(p);

 

the browser gave me this error :

 

SyntaxError: missing ; before statement

 

There is no ; that is missing !!!!!! the error was provoked because I put the word var twice . So why is the browser giving an error that does not exist? !!! I do not quite understand this error which has nothing to do with the error that I , intentionally, made namely putting the var twice

Link to comment
Share on other sites

So why is the browser giving an error that does not exist? !!! I do not quite understand this error which has nothing to do with the error that I , intentionally, made namely putting the var twice

Anything that executes code can only tell you what it thinks the problem is. It might not even report the error on the correct line. It's reading the code and trying to figure it out, and when it finds a problem it only tells you what it thinks the problem is. In that case, the problem is this line:
var p +=(150 &  14) << 1;
That line is a syntax error because you cannot use the += operator like that on the same line as you use var. When you use var you either end it with a semicolon without giving the variable a value (which is what the browser expects when it tells you there is a missing semicolon):
var p;
Or you use an equal sign to assign a value immediately:
var p =(150 &  14) << 1;
Trying to use an operator like += when you define a variable is a syntax error. The browser doesn't know what you meant to do, the only thing it can do is tell you what it thinks the problem is. It's your responsibility as the programmer to find the actual error and fix it. You're the smart one, the computer is not smart, it's up to you to know this stuff.
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...