[{"data":1,"prerenderedAt":645},["ShallowReactive",2],{"post:cfscript-gotchas":3},{"id":4,"title":5,"body":6,"date":631,"description":632,"draft":633,"extension":634,"meta":635,"navigation":636,"path":637,"seo":638,"stem":639,"tags":640,"__hash__":644},"posts\u002Fposts\u002Fcfscript-gotchas.md","CFScript Gotchas and Syntax Guides",{"type":7,"value":8,"toc":621},"minimark",[9,18,23,42,47,50,104,107,110,143,154,158,170,174,177,253,256,300,303,339,352,382,386,396,403,463,470,474,481,484,512,519,523,530,614,617],[10,11,12,13,17],"p",{},"Now, script syntax is ",[14,15,16],"em",{},"usually"," straightforward, but as with any language, sometimes it's easy to get confused over certain things in script syntax. In this post I'll cover a quick list of common \"gotchas\" in CFScript, document the proper syntax for each, and finally link to a list of CFScript guides which may help you start writing CFScript today.",[19,20,22],"h2",{"id":21},"common-gotchas-in-cfscript","Common \"Gotchas\" in CFScript",[24,25,26,30,33,36,39],"ul",{},[27,28,29],"li",{},"Too many pound signs",[27,31,32],{},"Missing semicolons",[27,34,35],{},"New operators",[27,37,38],{},"Assignment instead of equality operator",[27,40,41],{},"Strict equality operator",[43,44,46],"h3",{"id":45},"too-many-pound-signs","Too Many Pound Signs",[10,48,49],{},"Don't do this:",[51,52,57],"pre",{"className":53,"code":54,"language":55,"meta":56,"style":56},"language-js shiki shiki-themes github-light github-dark","if ( #isBroken# ) {\n    writeOutput( \"Hello #name#!\" );\n    writeOutput( #age# );\n}\n","js","",[58,59,60,73,90,98],"code",{"__ignoreMap":56},[61,62,65,69],"span",{"class":63,"line":64},"line",1,[61,66,68],{"class":67},"szBVR","if",[61,70,72],{"class":71},"sVt8B"," ( #isBroken# ) {\n",[61,74,76,80,83,87],{"class":63,"line":75},2,[61,77,79],{"class":78},"sScJk","    writeOutput",[61,81,82],{"class":71},"( ",[61,84,86],{"class":85},"sZZnC","\"Hello #name#!\"",[61,88,89],{"class":71}," );\n",[61,91,93,95],{"class":63,"line":92},3,[61,94,79],{"class":78},[61,96,97],{"class":71},"( #age# );\n",[61,99,101],{"class":63,"line":100},4,[61,102,103],{"class":71},"}\n",[10,105,106],{},"Why not? Because the pound signs are only necessary within strings. I suppose the correct term is \"string evaluation\", because your variable needs to be evaluated and the contents placed within the string.",[10,108,109],{},"Here's the proper syntax for that above example:",[51,111,113],{"className":53,"code":112,"language":55,"meta":56,"style":56},"if ( isBroken ) {\n    writeOutput( \"Hello #name#!\" );\n    writeOutput( age );\n}\n",[58,114,115,122,132,139],{"__ignoreMap":56},[61,116,117,119],{"class":63,"line":64},[61,118,68],{"class":67},[61,120,121],{"class":71}," ( isBroken ) {\n",[61,123,124,126,128,130],{"class":63,"line":75},[61,125,79],{"class":78},[61,127,82],{"class":71},[61,129,86],{"class":85},[61,131,89],{"class":71},[61,133,134,136],{"class":63,"line":92},[61,135,79],{"class":78},[61,137,138],{"class":71},"( age );\n",[61,140,141],{"class":63,"line":100},[61,142,103],{"class":71},[10,144,145,146,153],{},"For a more detailed writeup, check out Mark Kruger's ",[147,148,152],"a",{"href":149,"rel":150},"https:\u002F\u002Fwww.coldfusionmuse.com\u002Findex.cfm\u002F2011\u002F2\u002F10\u002Fwhen.do.i.use.pound.signs",[151],"nofollow","\"Bamboozled by Pound Sign Decisions\""," blog post.",[43,155,157],{"id":156},"missing-semicolons","Missing Semicolons",[10,159,160,161,165,166,169],{},"This is a fairly easy one. Semicolons are used to terminate a CFScript statement, and are ",[162,163,164],"strong",{},"required"," in ACF up to CF2016. Semicolons are optional now in CF2018, as well as Lucee. So if you get a cryptic ",[14,167,168],{},"\"Invalid construct. on line x\""," error, you may want to check your semicolons!",[43,171,173],{"id":172},"new-operators","New Operators",[10,175,176],{},"If you're using CFML's JS-like syntax, you should use the new JS-like operators as well, no? This is a matter of preference, but I feel that these operators are more \"standard\" in all languages and a consistent, improved experience for CF devs.",[24,178,179,189,197,205,213,221,229,237,245],{},[27,180,181,182,185,186],{},"Instead of ",[58,183,184],{},"NOT",", use ",[58,187,188],{},"!",[27,190,181,191,185,194],{},[58,192,193],{},"EQ",[58,195,196],{},"==",[27,198,181,199,185,202],{},[58,200,201],{},"NEQ",[58,203,204],{},"!=",[27,206,181,207,185,210],{},[58,208,209],{},"GT",[58,211,212],{},">",[27,214,181,215,185,218],{},[58,216,217],{},"LT",[58,219,220],{},"\u003C",[27,222,181,223,185,226],{},[58,224,225],{},"GTE",[58,227,228],{},">=",[27,230,181,231,185,234],{},[58,232,233],{},"LTE",[58,235,236],{},"\u003C=",[27,238,181,239,185,242],{},[58,240,241],{},"OR",[58,243,244],{},"||",[27,246,181,247,185,250],{},[58,248,249],{},"AND",[58,251,252],{},"&&",[10,254,255],{},"Older tag-based operators:",[51,257,259],{"className":53,"code":258,"language":55,"meta":56,"style":56},"if ( NOT json.success OR arrayLen(validation.errors) GT 0 ) {\n    \u002F\u002F do something\n}\n",[58,260,261,290,296],{"__ignoreMap":56},[61,262,263,265,268,271,274,276,279,282,284,287],{"class":63,"line":64},[61,264,68],{"class":67},[61,266,267],{"class":71}," ( ",[61,269,184],{"class":270},"sj4cs",[61,272,273],{"class":71}," json.success ",[61,275,241],{"class":270},[61,277,278],{"class":78}," arrayLen",[61,280,281],{"class":71},"(validation.errors) ",[61,283,209],{"class":270},[61,285,286],{"class":270}," 0",[61,288,289],{"class":71}," ) {\n",[61,291,292],{"class":63,"line":75},[61,293,295],{"class":294},"sJ8bj","    \u002F\u002F do something\n",[61,297,298],{"class":63,"line":92},[61,299,103],{"class":71},[10,301,302],{},"Newer, JS-like operators:",[51,304,306],{"className":53,"code":305,"language":55,"meta":56,"style":56},"if ( !json.success || arrayLen(validation.errors) > 0 ) {\n    \u002F\u002F do something\n}\n",[58,307,308,331,335],{"__ignoreMap":56},[61,309,310,312,314,316,319,321,323,325,327,329],{"class":63,"line":64},[61,311,68],{"class":67},[61,313,267],{"class":71},[61,315,188],{"class":67},[61,317,318],{"class":71},"json.success ",[61,320,244],{"class":67},[61,322,278],{"class":78},[61,324,281],{"class":71},[61,326,212],{"class":67},[61,328,286],{"class":270},[61,330,289],{"class":71},[61,332,333],{"class":63,"line":75},[61,334,295],{"class":294},[61,336,337],{"class":63,"line":92},[61,338,103],{"class":71},[10,340,341,342,345,346,351],{},"PS. ",[58,343,344],{},"ArrayLen()"," always returns a non-negative integer. There's really no need to check that it's greater than zero, because ",[147,347,350],{"href":348,"rel":349},"https:\u002F\u002Fwww.coldfusionmuse.com\u002Findex.cfm\u002F2010\u002F2\u002F5\u002FBooleans.and.Coldfusion",[151],"any non-zero integer will evaluate to true in CFML. Whereas zero always evaluates to false",". So you can shrink that conditional further like so:",[51,353,355],{"className":53,"code":354,"language":55,"meta":56,"style":56},"if ( !json.success || arrayLen(validation.errors) ) {\n    \u002F\u002F do something\n}\n",[58,356,357,374,378],{"__ignoreMap":56},[61,358,359,361,363,365,367,369,371],{"class":63,"line":64},[61,360,68],{"class":67},[61,362,267],{"class":71},[61,364,188],{"class":67},[61,366,318],{"class":71},[61,368,244],{"class":67},[61,370,278],{"class":78},[61,372,373],{"class":71},"(validation.errors) ) {\n",[61,375,376],{"class":63,"line":75},[61,377,295],{"class":294},[61,379,380],{"class":63,"line":92},[61,381,103],{"class":71},[19,383,385],{"id":384},"using-the-assignment-operator-instead-of-equality-operator","Using the Assignment Operator Instead of Equality Operator",[10,387,388,389,392,393,395],{},"I feel like this is a rite of passage for all developers. If you accidentally use the assignment operator (",[58,390,391],{},"=",") instead of the equality operator (",[58,394,196],{},") in a conditional, you will get some very interesting results.",[10,397,398,399,402],{},"For example, this code will always print ",[58,400,401],{},"True!"," in Lucee.",[51,404,406],{"className":53,"code":405,"language":55,"meta":56,"style":56},"if ( a = 1 ) {\n  writeOutput(\"True!\");\n} else {\n  writeOutput(\"False!\");\n}\n",[58,407,408,422,436,447,458],{"__ignoreMap":56},[61,409,410,412,415,417,420],{"class":63,"line":64},[61,411,68],{"class":67},[61,413,414],{"class":71}," ( a ",[61,416,391],{"class":67},[61,418,419],{"class":270}," 1",[61,421,289],{"class":71},[61,423,424,427,430,433],{"class":63,"line":75},[61,425,426],{"class":78},"  writeOutput",[61,428,429],{"class":71},"(",[61,431,432],{"class":85},"\"True!\"",[61,434,435],{"class":71},");\n",[61,437,438,441,444],{"class":63,"line":92},[61,439,440],{"class":71},"} ",[61,442,443],{"class":67},"else",[61,445,446],{"class":71}," {\n",[61,448,449,451,453,456],{"class":63,"line":100},[61,450,426],{"class":78},[61,452,429],{"class":71},[61,454,455],{"class":85},"\"False!\"",[61,457,435],{"class":71},[61,459,461],{"class":63,"line":460},5,[61,462,103],{"class":71},[10,464,465,466,469],{},"That is a tough bug to find, because it ",[14,467,468],{},"looks"," right upon first scan. So be intentional about how you write your conditionals!",[19,471,473],{"id":472},"strict-equality-operator","Strict Equality Operator",[10,475,476,477,480],{},"This is almost trick question - there ",[162,478,479],{},"is"," no \"strict equality\" operator in CFML!",[10,482,483],{},"So don't do this, because it just won't work:",[51,485,487],{"className":53,"code":486,"language":55,"meta":56,"style":56},"if ( myVar === \"theValue\" ) {\n    \u002F\u002F do something\n}\n",[58,488,489,504,508],{"__ignoreMap":56},[61,490,491,493,496,499,502],{"class":63,"line":64},[61,492,68],{"class":67},[61,494,495],{"class":71}," ( myVar ",[61,497,498],{"class":67},"===",[61,500,501],{"class":85}," \"theValue\"",[61,503,289],{"class":71},[61,505,506],{"class":63,"line":75},[61,507,295],{"class":294},[61,509,510],{"class":63,"line":92},[61,511,103],{"class":71},[10,513,514,515,518],{},"If you must enforce a type, use ",[58,516,517],{},"isValid()",".",[19,520,522],{"id":521},"cfscript-syntax-guides","CFScript Syntax Guides",[10,524,525,526,529],{},"I thought I'd put together a quick (non-exhaustive) list of CFScript syntax guides that may help folks on their journey to modern CF. You may want to bookmark a few of these, like Tony Junkes' comparison of tag and script syntax, as well as Ortus' \"100 Minutes\" full-on CFScript guide, and finally a cheat sheet by Pete Freitag. As you can see, there is a ",[162,527,528],{},"lot"," of CFScript documentation out there.",[24,531,532,540,553,566,578,586,594,602],{},[27,533,534,539],{},[147,535,538],{"href":536,"rel":537},"https:\u002F\u002Fgithub.com\u002Ftonyjunkes\u002Fcfml-tags-to-cfscript",[151],"CFML Tag to Script Conversions"," by Tony Junkes",[27,541,542,547,548],{},[147,543,546],{"href":544,"rel":545},"https:\u002F\u002Fwww.petefreitag.com\u002Fcheatsheets\u002Fcoldfusion\u002Fcfscript\u002F",[151],"CFScript Cheat Sheet"," by ",[147,549,552],{"href":550,"rel":551},"https:\u002F\u002Fwww.petefreitag.com",[151],"Pete Freitag",[27,554,555,560,561],{},[147,556,559],{"href":557,"rel":558},"https:\u002F\u002Fmodern-cfml.ortusbooks.com\u002F",[151],"Learn Modern CFML in 100 Minutes"," by the ",[147,562,565],{"href":563,"rel":564},"https:\u002F\u002Fcfdocs.org",[151],"Ortus Solutions",[27,567,568,573,574],{},[147,569,572],{"href":570,"rel":571},"https:\u002F\u002Fcfdocs.org\u002Fscript",[151],"CFScript Syntax Guide"," on ",[147,575,577],{"href":563,"rel":576},[151],"CFDocs.org",[27,579,580,585],{},[147,581,584],{"href":582,"rel":583},"https:\u002F\u002Fgithub.com\u002Fadamcameron\u002Fcfscript\u002Fblob\u002Fmaster\u002Fcfscript.md",[151],"Guide to CFScript"," by Adam Cameron",[27,587,588,593],{},[147,589,592],{"href":590,"rel":591},"https:\u002F\u002Fhelpx.adobe.com\u002Fcoldfusion\u002Fcfml-reference\u002Fcoldfusion-tags\u002Ftags-r-s\u002Fcfscript.html",[151],"CFScript Usage"," by Adobe",[27,595,596,601],{},[147,597,600],{"href":598,"rel":599},"https:\u002F\u002Fhelpx.adobe.com\u002Fcoldfusion\u002Fdeveloping-applications\u002Fthe-cfml-programming-language\u002Fextending-coldfusion-pages-with-cfml-scripting\u002Fusing-cfscript-statements.html",[151],"Using CFScript Statements",", also by Adobe",[27,603,604,609,610,613],{},[147,605,608],{"href":606,"rel":607},"https:\u002F\u002Fhelpx.adobe.com\u002Fcoldfusion\u002Fdeveloping-applications\u002Fthe-cfml-programming-language\u002Fextending-coldfusion-pages-with-cfml-scripting\u002Fthe-cfscript-language.html",[151],"The CFScript Language",", yet ",[14,611,612],{},"another"," Adobe CFScript guide",[10,615,616],{},"Hope this helps - thank you for reading!",[618,619,620],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":56,"searchDepth":75,"depth":75,"links":622},[623,628,629,630],{"id":21,"depth":75,"text":22,"children":624},[625,626,627],{"id":45,"depth":92,"text":46},{"id":156,"depth":92,"text":157},{"id":172,"depth":92,"text":173},{"id":384,"depth":75,"text":385},{"id":472,"depth":75,"text":473},{"id":521,"depth":75,"text":522},"2019-06-28T16:13:57-04:00","Now, script syntax is usually straightforward, but as with any language, sometimes it's easy to get confused over certain things in script syntax. In this post I'll cover a quick list of common \"gotchas\" in CFScript, document the proper syntax for each, and finally link to a list of CFScript guides which may help you start writing CFScript today.",false,"md",{},true,"\u002Fposts\u002Fcfscript-gotchas",{"title":5,"description":632},"posts\u002Fcfscript-gotchas",[641,642,643],"coldfusion","cfml","cfscript","FPxHZtDoFkINxjtup8f8Poo3Fux_p5sZPvtFdADxo-M",1790471527799]