Strings Regexps string also a string joiningstrings mad mad worldsplit

Strings regexps string also a string joiningstrings

This preview shows page 208 - 210 out of 517 pages.

Strings & Regexps ”string” , ’also a string’ , ’joining’+’strings’ ’mad, mad world’.split(/[, ]+/) == [”mad”,”mad”,”world”] ’mad, mad world’.slice(3,8)==”, mad” ; ’mad, mad world’.slice(-3)==”rld” ’mad’.indexOf(’d’)==2 , ’mad’.charAt(2)==’d’ , ’mad’.charCodeAt(4)==100 ’mad’.replace(/(\w)$/,’$1$1er’)==”madder” / regexp /.exec( string ) if no match returns null , if match returns array whose zeroth element is whole string matched and additional elements are parenthesized capture groups. string .match(/ regexp /) does the same, unless the /g regexp modifier is present. / regexp /.test( string ) (faster) returns true or false but no capture groups. Alternate constructor: new RegExp(’[Hh]e(l+)o’) Arrays var a = [1, {two: 2}, ’three’] ; a[1] == {two: 2} Zero-based, grow dynamically; objects whose keys are numbers (see Fallacies & Pitfalls) arr.sort(function (a,b) {. . . }) Function returns -1, 0 or 1 for a<b,a==b,a>b Numbers + - / % , also += , etc., ++ --, Math.pow( num,exp ) Math.round(n), Math.ceil(n), Math.floor(n) round their argument to nearest, higher, or lower integer respectively Math.random() returns a random number in (0,1) Conversions ’catch’+22==’catch22’ , ’4’+’11’==’411’ parseInt(’4oneone’)==4 , parseInt(’four11’)==NaN parseInt(’0101’,10)==101 , parseInt(’0101’,2)==5 , parseInt(’0101’)==65 (numbers beginning with 0 are parsed in octal by default, unless radix is specified) parseFloat(’1.1b23’)==1.1 , parseFloat(’1.1e3’)==1100 Booleans false , null , undefined (undefined value, different from null ), 0, the empty string ’’ , and NaN (not-a-number) are falsy (Boolean false); true and all other values are truthy . Naming localVar , local_var , ConstructorFunction , GLOBAL All are conventions; JavaScript has no specific capitalization rules. var keyword scopes variable to the function in which it appears, otherwise it becomes a global (technically, a property of the global object, as Section 6.3 describes). Variables don’t have types, but the objects they refer to do. Control flow while(), for(;;), if. . . else if. . . else, ?: (ternary operator), switch/case, try/catch/throw, return, break Statements separated by semicolons; interpreter tries to auto-insert “missing” ones, but this is perilous (see Fallacies & Pitfalls) Figure 6.2: Analogous to Figure 3.1 , this table summarizes basic constructs of JavaScript. See the text for important pitfalls. Whereas Ruby uses nil as both an explicit null value and the value returned for nonexistent instance variables, JavaScript distinguishes undefined ,
Image of page 208
which is returned for undeclared or unassigned variables, from the special value null and Boolean false . However, all three are “falsy”—they evaluate to false in a conditional. Figure 6.2 shows JavaScript’s basic syntax and constructs, which should look familiar to Java and Ruby programmers. The Fallacies & Pitfalls section describes several JavaScript pitfalls associated with the figure; read them carefully after you’ve finished this chapter, or you may find yourself banging your head against one of JavaScript’s unfortunate misfeatures or a JavaScript mechanism that looks and works almost but not quite like its Ruby counterpart. For example, whereas Ruby uses nil to mean both “undefined” (a variable that has never been given a value) and “empty” (a value that is always false),
Image of page 209
Image of page 210

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors